@aztec/validator-ha-signer 0.0.1-commit.a072138 → 0.0.1-commit.a89ec08
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 -0
- package/dest/config.d.ts +23 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +19 -0
- package/dest/db/in_memory.d.ts +20 -0
- package/dest/db/in_memory.d.ts.map +1 -0
- package/dest/db/in_memory.js +73 -0
- package/dest/db/postgres.d.ts +17 -3
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +32 -5
- 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 +7 -2
- package/dest/db/types.d.ts.map +1 -1
- package/dest/factory.d.ts +14 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +25 -0
- package/dest/slashing_protection_service.d.ts +8 -4
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +43 -14
- package/dest/types.d.ts +18 -5
- package/dest/types.d.ts.map +1 -1
- package/dest/validator_ha_signer.d.ts +4 -3
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +8 -3
- package/package.json +3 -2
- package/src/config.ts +24 -0
- package/src/db/in_memory.ts +107 -0
- package/src/db/postgres.ts +33 -2
- package/src/db/schema.ts +41 -22
- package/src/db/types.ts +6 -1
- package/src/factory.ts +32 -0
- package/src/slashing_protection_service.ts +54 -17
- package/src/types.ts +19 -0
- package/src/validator_ha_signer.ts +8 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-ha-signer",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.a89ec08",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./config": "./dest/config.js",
|
|
@@ -74,7 +74,8 @@
|
|
|
74
74
|
]
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
-
"@aztec/
|
|
77
|
+
"@aztec/ethereum": "0.0.1-commit.a89ec08",
|
|
78
|
+
"@aztec/foundation": "0.0.1-commit.a89ec08",
|
|
78
79
|
"node-pg-migrate": "^8.0.4",
|
|
79
80
|
"pg": "^8.11.3",
|
|
80
81
|
"tslib": "^2.4.0",
|
package/src/config.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
|
|
1
2
|
import {
|
|
2
3
|
type ConfigMappingsType,
|
|
3
4
|
booleanConfigHelper,
|
|
@@ -6,6 +7,7 @@ import {
|
|
|
6
7
|
numberConfigHelper,
|
|
7
8
|
optionalNumberConfigHelper,
|
|
8
9
|
} from '@aztec/foundation/config';
|
|
10
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
9
11
|
import type { ZodFor } from '@aztec/foundation/schemas';
|
|
10
12
|
|
|
11
13
|
import { z } from 'zod';
|
|
@@ -19,6 +21,8 @@ import { z } from 'zod';
|
|
|
19
21
|
export interface ValidatorHASignerConfig {
|
|
20
22
|
/** Whether HA signing / slashing protection is enabled */
|
|
21
23
|
haSigningEnabled: boolean;
|
|
24
|
+
/** L1 contract addresses (rollup address required) */
|
|
25
|
+
l1Contracts: Pick<L1ContractAddresses, 'rollupAddress'>;
|
|
22
26
|
/** Unique identifier for this node */
|
|
23
27
|
nodeId: string;
|
|
24
28
|
/** How long to wait between polls when a duty is being signed (ms) */
|
|
@@ -27,6 +31,8 @@ export interface ValidatorHASignerConfig {
|
|
|
27
31
|
signingTimeoutMs: number;
|
|
28
32
|
/** Maximum age of a stuck duty in ms (defaults to 2x hardcoded Aztec slot duration if not set) */
|
|
29
33
|
maxStuckDutiesAgeMs?: number;
|
|
34
|
+
/** Optional: clean up old duties after this many hours (disabled if not set) */
|
|
35
|
+
cleanupOldDutiesAfterHours?: number;
|
|
30
36
|
/**
|
|
31
37
|
* PostgreSQL connection string
|
|
32
38
|
* Format: postgresql://user:password@host:port/database
|
|
@@ -51,6 +57,15 @@ export const validatorHASignerConfigMappings: ConfigMappingsType<ValidatorHASign
|
|
|
51
57
|
description: 'Whether HA signing / slashing protection is enabled',
|
|
52
58
|
...booleanConfigHelper(false),
|
|
53
59
|
},
|
|
60
|
+
l1Contracts: {
|
|
61
|
+
description: 'L1 contract addresses (rollup address required)',
|
|
62
|
+
nested: {
|
|
63
|
+
rollupAddress: {
|
|
64
|
+
description: 'The Ethereum address of the rollup contract (must be set programmatically)',
|
|
65
|
+
parseEnv: (val: string) => EthAddress.fromString(val),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
54
69
|
nodeId: {
|
|
55
70
|
env: 'VALIDATOR_HA_NODE_ID',
|
|
56
71
|
description: 'The unique identifier for this node',
|
|
@@ -71,6 +86,11 @@ export const validatorHASignerConfigMappings: ConfigMappingsType<ValidatorHASign
|
|
|
71
86
|
description: 'The maximum age of a stuck duty in ms (defaults to 2x Aztec slot duration)',
|
|
72
87
|
...optionalNumberConfigHelper(),
|
|
73
88
|
},
|
|
89
|
+
cleanupOldDutiesAfterHours: {
|
|
90
|
+
env: 'VALIDATOR_HA_OLD_DUTIES_MAX_AGE_H',
|
|
91
|
+
description: 'Optional: clean up old duties after this many hours (disabled if not set)',
|
|
92
|
+
...optionalNumberConfigHelper(),
|
|
93
|
+
},
|
|
74
94
|
databaseUrl: {
|
|
75
95
|
env: 'VALIDATOR_HA_DATABASE_URL',
|
|
76
96
|
description:
|
|
@@ -113,10 +133,14 @@ export function getConfigEnvVars(): ValidatorHASignerConfig {
|
|
|
113
133
|
|
|
114
134
|
export const ValidatorHASignerConfigSchema = z.object({
|
|
115
135
|
haSigningEnabled: z.boolean(),
|
|
136
|
+
l1Contracts: z.object({
|
|
137
|
+
rollupAddress: z.instanceof(EthAddress),
|
|
138
|
+
}),
|
|
116
139
|
nodeId: z.string(),
|
|
117
140
|
pollingIntervalMs: z.number().min(0),
|
|
118
141
|
signingTimeoutMs: z.number().min(0),
|
|
119
142
|
maxStuckDutiesAgeMs: z.number().min(0).optional(),
|
|
143
|
+
cleanupOldDutiesAfterHours: z.number().min(0).optional(),
|
|
120
144
|
databaseUrl: z.string().optional(),
|
|
121
145
|
poolMaxCount: z.number().min(0).optional(),
|
|
122
146
|
poolMinCount: z.number().min(0).optional(),
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory implementation of SlashingProtectionDatabase for testing.
|
|
3
|
+
* Used to simulate shared slashing protection in HA test setups without requiring PostgreSQL.
|
|
4
|
+
*/
|
|
5
|
+
import type { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
6
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
7
|
+
|
|
8
|
+
import type { SlashingProtectionDatabase, TryInsertOrGetResult, ValidatorDutyRecord } from '../types.js';
|
|
9
|
+
import type { CheckAndRecordParams, DutyType } from './types.js';
|
|
10
|
+
import { DutyStatus, getBlockIndexFromDutyIdentifier } from './types.js';
|
|
11
|
+
|
|
12
|
+
/** Creates a unique key for a duty based on its identifying fields. */
|
|
13
|
+
function dutyKey(
|
|
14
|
+
rollupAddress: EthAddress,
|
|
15
|
+
validatorAddress: EthAddress,
|
|
16
|
+
slot: SlotNumber,
|
|
17
|
+
dutyType: DutyType,
|
|
18
|
+
blockIndexWithinCheckpoint: number,
|
|
19
|
+
): string {
|
|
20
|
+
return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** In-memory slashing protection database for testing HA setups. */
|
|
24
|
+
export class InMemorySlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
25
|
+
private duties = new Map<string, ValidatorDutyRecord>();
|
|
26
|
+
|
|
27
|
+
tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
|
|
28
|
+
const blockIndex = getBlockIndexFromDutyIdentifier(params);
|
|
29
|
+
const key = dutyKey(params.rollupAddress, params.validatorAddress, params.slot, params.dutyType, blockIndex);
|
|
30
|
+
|
|
31
|
+
const existing = this.duties.get(key);
|
|
32
|
+
if (existing) {
|
|
33
|
+
return Promise.resolve({ isNew: false, record: existing });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const lockToken = `lock-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
37
|
+
const record: ValidatorDutyRecord = {
|
|
38
|
+
rollupAddress: params.rollupAddress,
|
|
39
|
+
validatorAddress: params.validatorAddress,
|
|
40
|
+
slot: params.slot,
|
|
41
|
+
blockNumber: params.blockNumber as BlockNumber,
|
|
42
|
+
blockIndexWithinCheckpoint: blockIndex,
|
|
43
|
+
dutyType: params.dutyType,
|
|
44
|
+
status: DutyStatus.SIGNING,
|
|
45
|
+
messageHash: params.messageHash,
|
|
46
|
+
nodeId: params.nodeId,
|
|
47
|
+
lockToken,
|
|
48
|
+
startedAt: new Date(),
|
|
49
|
+
};
|
|
50
|
+
this.duties.set(key, record);
|
|
51
|
+
return Promise.resolve({ isNew: true, record });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
updateDutySigned(
|
|
55
|
+
rollupAddress: EthAddress,
|
|
56
|
+
validatorAddress: EthAddress,
|
|
57
|
+
slot: SlotNumber,
|
|
58
|
+
dutyType: DutyType,
|
|
59
|
+
signature: string,
|
|
60
|
+
lockToken: string,
|
|
61
|
+
blockIndexWithinCheckpoint: number,
|
|
62
|
+
): Promise<boolean> {
|
|
63
|
+
const key = dutyKey(rollupAddress, validatorAddress, slot, dutyType, blockIndexWithinCheckpoint);
|
|
64
|
+
const record = this.duties.get(key);
|
|
65
|
+
if (!record || record.lockToken !== lockToken) {
|
|
66
|
+
return Promise.resolve(false);
|
|
67
|
+
}
|
|
68
|
+
record.status = DutyStatus.SIGNED;
|
|
69
|
+
record.signature = signature;
|
|
70
|
+
record.completedAt = new Date();
|
|
71
|
+
return Promise.resolve(true);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
deleteDuty(
|
|
75
|
+
rollupAddress: EthAddress,
|
|
76
|
+
validatorAddress: EthAddress,
|
|
77
|
+
slot: SlotNumber,
|
|
78
|
+
dutyType: DutyType,
|
|
79
|
+
lockToken: string,
|
|
80
|
+
blockIndexWithinCheckpoint: number,
|
|
81
|
+
): Promise<boolean> {
|
|
82
|
+
const key = dutyKey(rollupAddress, validatorAddress, slot, dutyType, blockIndexWithinCheckpoint);
|
|
83
|
+
const record = this.duties.get(key);
|
|
84
|
+
if (!record || record.lockToken !== lockToken) {
|
|
85
|
+
return Promise.resolve(false);
|
|
86
|
+
}
|
|
87
|
+
this.duties.delete(key);
|
|
88
|
+
return Promise.resolve(true);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
cleanupOwnStuckDuties(_nodeId: string, _maxAgeMs: number): Promise<number> {
|
|
92
|
+
return Promise.resolve(0);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
|
|
96
|
+
return Promise.resolve(0);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
cleanupOldDuties(_maxAgeMs: number): Promise<number> {
|
|
100
|
+
return Promise.resolve(0);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
close(): Promise<void> {
|
|
104
|
+
this.duties.clear();
|
|
105
|
+
return Promise.resolve();
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/db/postgres.ts
CHANGED
|
@@ -11,6 +11,8 @@ import type { QueryResult, QueryResultRow } from 'pg';
|
|
|
11
11
|
|
|
12
12
|
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
13
13
|
import {
|
|
14
|
+
CLEANUP_OLD_DUTIES,
|
|
15
|
+
CLEANUP_OUTDATED_ROLLUP_DUTIES,
|
|
14
16
|
CLEANUP_OWN_STUCK_DUTIES,
|
|
15
17
|
DELETE_DUTY,
|
|
16
18
|
INSERT_OR_GET_DUTY,
|
|
@@ -101,6 +103,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
101
103
|
const result = await retry<QueryResult<InsertOrGetRow>>(
|
|
102
104
|
async () => {
|
|
103
105
|
const queryResult: QueryResult<InsertOrGetRow> = await this.pool.query(INSERT_OR_GET_DUTY, [
|
|
106
|
+
params.rollupAddress.toString(),
|
|
104
107
|
params.validatorAddress.toString(),
|
|
105
108
|
params.slot.toString(),
|
|
106
109
|
params.blockNumber.toString(),
|
|
@@ -148,6 +151,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
148
151
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
149
152
|
*/
|
|
150
153
|
async updateDutySigned(
|
|
154
|
+
rollupAddress: EthAddress,
|
|
151
155
|
validatorAddress: EthAddress,
|
|
152
156
|
slot: SlotNumber,
|
|
153
157
|
dutyType: DutyType,
|
|
@@ -157,6 +161,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
157
161
|
): Promise<boolean> {
|
|
158
162
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
159
163
|
signature,
|
|
164
|
+
rollupAddress.toString(),
|
|
160
165
|
validatorAddress.toString(),
|
|
161
166
|
slot.toString(),
|
|
162
167
|
dutyType,
|
|
@@ -166,6 +171,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
166
171
|
|
|
167
172
|
if (result.rowCount === 0) {
|
|
168
173
|
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
174
|
+
rollupAddress: rollupAddress.toString(),
|
|
169
175
|
validatorAddress: validatorAddress.toString(),
|
|
170
176
|
slot: slot.toString(),
|
|
171
177
|
dutyType,
|
|
@@ -184,6 +190,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
184
190
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
185
191
|
*/
|
|
186
192
|
async deleteDuty(
|
|
193
|
+
rollupAddress: EthAddress,
|
|
187
194
|
validatorAddress: EthAddress,
|
|
188
195
|
slot: SlotNumber,
|
|
189
196
|
dutyType: DutyType,
|
|
@@ -191,6 +198,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
191
198
|
blockIndexWithinCheckpoint: number,
|
|
192
199
|
): Promise<boolean> {
|
|
193
200
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
201
|
+
rollupAddress.toString(),
|
|
194
202
|
validatorAddress.toString(),
|
|
195
203
|
slot.toString(),
|
|
196
204
|
dutyType,
|
|
@@ -200,6 +208,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
200
208
|
|
|
201
209
|
if (result.rowCount === 0) {
|
|
202
210
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
211
|
+
rollupAddress: rollupAddress.toString(),
|
|
203
212
|
validatorAddress: validatorAddress.toString(),
|
|
204
213
|
slot: slot.toString(),
|
|
205
214
|
dutyType,
|
|
@@ -215,6 +224,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
215
224
|
*/
|
|
216
225
|
private rowToRecord(row: DutyRow): ValidatorDutyRecord {
|
|
217
226
|
return {
|
|
227
|
+
rollupAddress: EthAddress.fromString(row.rollup_address),
|
|
218
228
|
validatorAddress: EthAddress.fromString(row.validator_address),
|
|
219
229
|
slot: SlotNumber.fromString(row.slot),
|
|
220
230
|
blockNumber: BlockNumber.fromString(row.block_number),
|
|
@@ -244,8 +254,29 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
244
254
|
* @returns the number of duties cleaned up
|
|
245
255
|
*/
|
|
246
256
|
async cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
247
|
-
const
|
|
248
|
-
|
|
257
|
+
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, maxAgeMs]);
|
|
258
|
+
return result.rowCount ?? 0;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Cleanup duties with outdated rollup address.
|
|
263
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
264
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
265
|
+
* @returns the number of duties cleaned up
|
|
266
|
+
*/
|
|
267
|
+
async cleanupOutdatedRollupDuties(currentRollupAddress: EthAddress): Promise<number> {
|
|
268
|
+
const result = await this.pool.query(CLEANUP_OUTDATED_ROLLUP_DUTIES, [currentRollupAddress.toString()]);
|
|
269
|
+
return result.rowCount ?? 0;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Cleanup old signed duties.
|
|
274
|
+
* Removes only signed duties older than the specified age.
|
|
275
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
276
|
+
* @returns the number of duties cleaned up
|
|
277
|
+
*/
|
|
278
|
+
async cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
279
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [maxAgeMs]);
|
|
249
280
|
return result.rowCount ?? 0;
|
|
250
281
|
}
|
|
251
282
|
}
|
package/src/db/schema.ts
CHANGED
|
@@ -16,12 +16,13 @@ export const SCHEMA_VERSION = 1;
|
|
|
16
16
|
*/
|
|
17
17
|
export const CREATE_VALIDATOR_DUTIES_TABLE = `
|
|
18
18
|
CREATE TABLE IF NOT EXISTS validator_duties (
|
|
19
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
19
20
|
validator_address VARCHAR(42) NOT NULL,
|
|
20
21
|
slot BIGINT NOT NULL,
|
|
21
22
|
block_number BIGINT NOT NULL,
|
|
22
23
|
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
23
24
|
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
24
|
-
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed'
|
|
25
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
25
26
|
message_hash VARCHAR(66) NOT NULL,
|
|
26
27
|
signature VARCHAR(132),
|
|
27
28
|
node_id VARCHAR(255) NOT NULL,
|
|
@@ -30,7 +31,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
|
|
|
30
31
|
completed_at TIMESTAMP,
|
|
31
32
|
error_message TEXT,
|
|
32
33
|
|
|
33
|
-
PRIMARY KEY (validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
34
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
34
35
|
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
35
36
|
);
|
|
36
37
|
`;
|
|
@@ -101,6 +102,7 @@ SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
|
101
102
|
export const INSERT_OR_GET_DUTY = `
|
|
102
103
|
WITH inserted AS (
|
|
103
104
|
INSERT INTO validator_duties (
|
|
105
|
+
rollup_address,
|
|
104
106
|
validator_address,
|
|
105
107
|
slot,
|
|
106
108
|
block_number,
|
|
@@ -111,9 +113,10 @@ WITH inserted AS (
|
|
|
111
113
|
node_id,
|
|
112
114
|
lock_token,
|
|
113
115
|
started_at
|
|
114
|
-
) VALUES ($1, $2, $3, $4, $5, 'signing', $
|
|
115
|
-
ON CONFLICT (validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
116
|
+
) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)
|
|
117
|
+
ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
116
118
|
RETURNING
|
|
119
|
+
rollup_address,
|
|
117
120
|
validator_address,
|
|
118
121
|
slot,
|
|
119
122
|
block_number,
|
|
@@ -132,6 +135,7 @@ WITH inserted AS (
|
|
|
132
135
|
SELECT * FROM inserted
|
|
133
136
|
UNION ALL
|
|
134
137
|
SELECT
|
|
138
|
+
rollup_address,
|
|
135
139
|
validator_address,
|
|
136
140
|
slot,
|
|
137
141
|
block_number,
|
|
@@ -147,10 +151,11 @@ SELECT
|
|
|
147
151
|
error_message,
|
|
148
152
|
FALSE as is_new
|
|
149
153
|
FROM validator_duties
|
|
150
|
-
WHERE
|
|
151
|
-
AND
|
|
152
|
-
AND
|
|
153
|
-
AND
|
|
154
|
+
WHERE rollup_address = $1
|
|
155
|
+
AND validator_address = $2
|
|
156
|
+
AND slot = $3
|
|
157
|
+
AND duty_type = $6
|
|
158
|
+
AND block_index_within_checkpoint = $5
|
|
154
159
|
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
155
160
|
`;
|
|
156
161
|
|
|
@@ -162,12 +167,13 @@ UPDATE validator_duties
|
|
|
162
167
|
SET status = 'signed',
|
|
163
168
|
signature = $1,
|
|
164
169
|
completed_at = CURRENT_TIMESTAMP
|
|
165
|
-
WHERE
|
|
166
|
-
AND
|
|
167
|
-
AND
|
|
168
|
-
AND
|
|
170
|
+
WHERE rollup_address = $2
|
|
171
|
+
AND validator_address = $3
|
|
172
|
+
AND slot = $4
|
|
173
|
+
AND duty_type = $5
|
|
174
|
+
AND block_index_within_checkpoint = $6
|
|
169
175
|
AND status = 'signing'
|
|
170
|
-
AND lock_token = $
|
|
176
|
+
AND lock_token = $7;
|
|
171
177
|
`;
|
|
172
178
|
|
|
173
179
|
/**
|
|
@@ -176,12 +182,13 @@ WHERE validator_address = $2
|
|
|
176
182
|
*/
|
|
177
183
|
export const DELETE_DUTY = `
|
|
178
184
|
DELETE FROM validator_duties
|
|
179
|
-
WHERE
|
|
180
|
-
AND
|
|
181
|
-
AND
|
|
182
|
-
AND
|
|
185
|
+
WHERE rollup_address = $1
|
|
186
|
+
AND validator_address = $2
|
|
187
|
+
AND slot = $3
|
|
188
|
+
AND duty_type = $4
|
|
189
|
+
AND block_index_within_checkpoint = $5
|
|
183
190
|
AND status = 'signing'
|
|
184
|
-
AND lock_token = $
|
|
191
|
+
AND lock_token = $6;
|
|
185
192
|
`;
|
|
186
193
|
|
|
187
194
|
/**
|
|
@@ -196,23 +203,34 @@ WHERE status = 'signed'
|
|
|
196
203
|
|
|
197
204
|
/**
|
|
198
205
|
* Query to clean up old duties (for maintenance)
|
|
199
|
-
* Removes duties older than a specified
|
|
206
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
200
207
|
*/
|
|
201
208
|
export const CLEANUP_OLD_DUTIES = `
|
|
202
209
|
DELETE FROM validator_duties
|
|
203
|
-
WHERE status
|
|
204
|
-
AND started_at < $1;
|
|
210
|
+
WHERE status = 'signed'
|
|
211
|
+
AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
|
|
205
212
|
`;
|
|
206
213
|
|
|
207
214
|
/**
|
|
208
215
|
* Query to cleanup own stuck duties
|
|
209
216
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
217
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
210
218
|
*/
|
|
211
219
|
export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
212
220
|
DELETE FROM validator_duties
|
|
213
221
|
WHERE node_id = $1
|
|
214
222
|
AND status = 'signing'
|
|
215
|
-
AND started_at < $2;
|
|
223
|
+
AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
|
|
224
|
+
`;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Query to cleanup duties with outdated rollup address
|
|
228
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
229
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
230
|
+
*/
|
|
231
|
+
export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
|
|
232
|
+
DELETE FROM validator_duties
|
|
233
|
+
WHERE rollup_address != $1;
|
|
216
234
|
`;
|
|
217
235
|
|
|
218
236
|
/**
|
|
@@ -231,6 +249,7 @@ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
|
|
|
231
249
|
*/
|
|
232
250
|
export const GET_STUCK_DUTIES = `
|
|
233
251
|
SELECT
|
|
252
|
+
rollup_address,
|
|
234
253
|
validator_address,
|
|
235
254
|
slot,
|
|
236
255
|
block_number,
|
package/src/db/types.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { Signature } from '@aztec/foundation/eth-signature';
|
|
|
6
6
|
* Row type from PostgreSQL query
|
|
7
7
|
*/
|
|
8
8
|
export interface DutyRow {
|
|
9
|
+
rollup_address: string;
|
|
9
10
|
validator_address: string;
|
|
10
11
|
slot: string;
|
|
11
12
|
block_number: string;
|
|
@@ -54,6 +55,8 @@ export enum DutyStatus {
|
|
|
54
55
|
* Record of a validator duty in the database
|
|
55
56
|
*/
|
|
56
57
|
export interface ValidatorDutyRecord {
|
|
58
|
+
/** Ethereum address of the rollup contract */
|
|
59
|
+
rollupAddress: EthAddress;
|
|
57
60
|
/** Ethereum address of the validator */
|
|
58
61
|
validatorAddress: EthAddress;
|
|
59
62
|
/** Slot number for this duty */
|
|
@@ -78,7 +81,7 @@ export interface ValidatorDutyRecord {
|
|
|
78
81
|
startedAt: Date;
|
|
79
82
|
/** When the duty signing was completed (success or failure) */
|
|
80
83
|
completedAt?: Date;
|
|
81
|
-
/** Error message
|
|
84
|
+
/** Error message (currently unused) */
|
|
82
85
|
errorMessage?: string;
|
|
83
86
|
}
|
|
84
87
|
|
|
@@ -87,6 +90,7 @@ export interface ValidatorDutyRecord {
|
|
|
87
90
|
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
88
91
|
*/
|
|
89
92
|
export interface BlockProposalDutyIdentifier {
|
|
93
|
+
rollupAddress: EthAddress;
|
|
90
94
|
validatorAddress: EthAddress;
|
|
91
95
|
slot: SlotNumber;
|
|
92
96
|
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
@@ -99,6 +103,7 @@ export interface BlockProposalDutyIdentifier {
|
|
|
99
103
|
* blockIndexWithinCheckpoint is not applicable (internally stored as -1).
|
|
100
104
|
*/
|
|
101
105
|
export interface OtherDutyIdentifier {
|
|
106
|
+
rollupAddress: EthAddress;
|
|
102
107
|
validatorAddress: EthAddress;
|
|
103
108
|
slot: SlotNumber;
|
|
104
109
|
dutyType:
|
package/src/factory.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { Pool } from 'pg';
|
|
5
5
|
|
|
6
6
|
import type { ValidatorHASignerConfig } from './config.js';
|
|
7
|
+
import { InMemorySlashingProtectionDatabase } from './db/in_memory.js';
|
|
7
8
|
import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
|
|
8
9
|
import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
|
|
9
10
|
import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
@@ -80,3 +81,34 @@ export async function createHASigner(
|
|
|
80
81
|
|
|
81
82
|
return { signer, db };
|
|
82
83
|
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create an in-memory SlashingProtectionDatabase that can be shared across
|
|
87
|
+
* multiple validator nodes in the same process. Used for testing HA setups.
|
|
88
|
+
*/
|
|
89
|
+
export function createSharedSlashingProtectionDb(): SlashingProtectionDatabase {
|
|
90
|
+
return new InMemorySlashingProtectionDatabase();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
|
|
95
|
+
* Used for testing HA setups where multiple nodes share the same protection database.
|
|
96
|
+
*/
|
|
97
|
+
export function createSignerFromSharedDb(
|
|
98
|
+
db: SlashingProtectionDatabase,
|
|
99
|
+
config: Pick<
|
|
100
|
+
ValidatorHASignerConfig,
|
|
101
|
+
'nodeId' | 'pollingIntervalMs' | 'signingTimeoutMs' | 'maxStuckDutiesAgeMs' | 'l1Contracts'
|
|
102
|
+
>,
|
|
103
|
+
): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
|
|
104
|
+
const signerConfig: ValidatorHASignerConfig = {
|
|
105
|
+
haSigningEnabled: true,
|
|
106
|
+
l1Contracts: config.l1Contracts,
|
|
107
|
+
nodeId: config.nodeId || `shared-${Date.now()}`,
|
|
108
|
+
pollingIntervalMs: config.pollingIntervalMs ?? 100,
|
|
109
|
+
signingTimeoutMs: config.signingTimeoutMs ?? 3000,
|
|
110
|
+
maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs,
|
|
111
|
+
};
|
|
112
|
+
const signer = new ValidatorHASigner(db, signerConfig);
|
|
113
|
+
return { signer, db };
|
|
114
|
+
}
|
|
@@ -40,6 +40,7 @@ export class SlashingProtectionService {
|
|
|
40
40
|
private readonly maxStuckDutiesAgeMs: number;
|
|
41
41
|
|
|
42
42
|
private cleanupRunningPromise: RunningPromise;
|
|
43
|
+
private lastOldDutiesCleanupAtMs?: number;
|
|
43
44
|
|
|
44
45
|
constructor(
|
|
45
46
|
private readonly db: SlashingProtectionDatabase,
|
|
@@ -51,11 +52,7 @@ export class SlashingProtectionService {
|
|
|
51
52
|
// Default to 144s (2x 72s Aztec slot duration) if not explicitly configured
|
|
52
53
|
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
53
54
|
|
|
54
|
-
this.cleanupRunningPromise = new RunningPromise(
|
|
55
|
-
this.cleanupStuckDuties.bind(this),
|
|
56
|
-
this.log,
|
|
57
|
-
this.maxStuckDutiesAgeMs,
|
|
58
|
-
);
|
|
55
|
+
this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
|
|
59
56
|
}
|
|
60
57
|
|
|
61
58
|
/**
|
|
@@ -67,7 +64,6 @@ export class SlashingProtectionService {
|
|
|
67
64
|
* 2. If insert succeeds, we acquired the lock - return the lockToken
|
|
68
65
|
* 3. If a record exists, handle based on status:
|
|
69
66
|
* - SIGNED: Throw appropriate error (already signed or slashing protection)
|
|
70
|
-
* - FAILED: Delete the failed record
|
|
71
67
|
* - SIGNING: Wait and poll until status changes, then handle result
|
|
72
68
|
*
|
|
73
69
|
* @returns The lockToken that must be used for recordSuccess/deleteDuty
|
|
@@ -89,7 +85,7 @@ export class SlashingProtectionService {
|
|
|
89
85
|
|
|
90
86
|
if (isNew) {
|
|
91
87
|
// We successfully acquired the lock
|
|
92
|
-
this.log.
|
|
88
|
+
this.log.verbose(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
|
|
93
89
|
validatorAddress: validatorAddress.toString(),
|
|
94
90
|
nodeId,
|
|
95
91
|
});
|
|
@@ -149,10 +145,11 @@ export class SlashingProtectionService {
|
|
|
149
145
|
* @returns true if the update succeeded, false if token didn't match
|
|
150
146
|
*/
|
|
151
147
|
async recordSuccess(params: RecordSuccessParams): Promise<boolean> {
|
|
152
|
-
const { validatorAddress, slot, dutyType, signature, nodeId, lockToken } = params;
|
|
148
|
+
const { rollupAddress, validatorAddress, slot, dutyType, signature, nodeId, lockToken } = params;
|
|
153
149
|
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
154
150
|
|
|
155
151
|
const success = await this.db.updateDutySigned(
|
|
152
|
+
rollupAddress,
|
|
156
153
|
validatorAddress,
|
|
157
154
|
slot,
|
|
158
155
|
dutyType,
|
|
@@ -162,7 +159,7 @@ export class SlashingProtectionService {
|
|
|
162
159
|
);
|
|
163
160
|
|
|
164
161
|
if (success) {
|
|
165
|
-
this.log.
|
|
162
|
+
this.log.verbose(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
|
|
166
163
|
validatorAddress: validatorAddress.toString(),
|
|
167
164
|
nodeId,
|
|
168
165
|
});
|
|
@@ -184,10 +181,17 @@ export class SlashingProtectionService {
|
|
|
184
181
|
* @returns true if the delete succeeded, false if token didn't match
|
|
185
182
|
*/
|
|
186
183
|
async deleteDuty(params: DeleteDutyParams): Promise<boolean> {
|
|
187
|
-
const { validatorAddress, slot, dutyType, lockToken } = params;
|
|
184
|
+
const { rollupAddress, validatorAddress, slot, dutyType, lockToken } = params;
|
|
188
185
|
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
189
186
|
|
|
190
|
-
const success = await this.db.deleteDuty(
|
|
187
|
+
const success = await this.db.deleteDuty(
|
|
188
|
+
rollupAddress,
|
|
189
|
+
validatorAddress,
|
|
190
|
+
slot,
|
|
191
|
+
dutyType,
|
|
192
|
+
lockToken,
|
|
193
|
+
blockIndexWithinCheckpoint,
|
|
194
|
+
);
|
|
191
195
|
|
|
192
196
|
if (success) {
|
|
193
197
|
this.log.info(`Deleted duty ${dutyType} at slot ${slot} to allow retry`, {
|
|
@@ -213,7 +217,19 @@ export class SlashingProtectionService {
|
|
|
213
217
|
* Start running tasks.
|
|
214
218
|
* Cleanup runs immediately on start to recover from any previous crashes.
|
|
215
219
|
*/
|
|
216
|
-
|
|
220
|
+
/**
|
|
221
|
+
* Start the background cleanup task.
|
|
222
|
+
* Also performs one-time cleanup of duties with outdated rollup addresses.
|
|
223
|
+
*/
|
|
224
|
+
async start() {
|
|
225
|
+
// One-time cleanup at startup: remove duties from previous rollup versions
|
|
226
|
+
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.l1Contracts.rollupAddress);
|
|
227
|
+
if (numOutdatedRollupDuties > 0) {
|
|
228
|
+
this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
|
|
229
|
+
currentRollupAddress: this.config.l1Contracts.rollupAddress.toString(),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
217
233
|
this.cleanupRunningPromise.start();
|
|
218
234
|
this.log.info('Slashing protection service started', { nodeId: this.config.nodeId });
|
|
219
235
|
}
|
|
@@ -236,15 +252,36 @@ export class SlashingProtectionService {
|
|
|
236
252
|
}
|
|
237
253
|
|
|
238
254
|
/**
|
|
239
|
-
*
|
|
255
|
+
* Periodic cleanup of stuck duties and optionally old signed duties.
|
|
256
|
+
* Runs in the background via RunningPromise.
|
|
240
257
|
*/
|
|
241
|
-
private async
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
258
|
+
private async cleanup() {
|
|
259
|
+
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status)
|
|
260
|
+
const numStuckDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
|
|
261
|
+
if (numStuckDuties > 0) {
|
|
262
|
+
this.log.verbose(`Cleaned up ${numStuckDuties} stuck duties`, {
|
|
245
263
|
nodeId: this.config.nodeId,
|
|
246
264
|
maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs,
|
|
247
265
|
});
|
|
248
266
|
}
|
|
267
|
+
|
|
268
|
+
// 2. Clean up old signed duties if configured
|
|
269
|
+
// we shouldn't run this as often as stuck duty cleanup.
|
|
270
|
+
if (this.config.cleanupOldDutiesAfterHours !== undefined) {
|
|
271
|
+
const maxAgeMs = this.config.cleanupOldDutiesAfterHours * 60 * 60 * 1000;
|
|
272
|
+
const nowMs = Date.now();
|
|
273
|
+
const shouldRun =
|
|
274
|
+
this.lastOldDutiesCleanupAtMs === undefined || nowMs - this.lastOldDutiesCleanupAtMs >= maxAgeMs;
|
|
275
|
+
if (shouldRun) {
|
|
276
|
+
const numOldDuties = await this.db.cleanupOldDuties(maxAgeMs);
|
|
277
|
+
this.lastOldDutiesCleanupAtMs = nowMs;
|
|
278
|
+
if (numOldDuties > 0) {
|
|
279
|
+
this.log.verbose(`Cleaned up ${numOldDuties} old signed duties`, {
|
|
280
|
+
cleanupOldDutiesAfterHours: this.config.cleanupOldDutiesAfterHours,
|
|
281
|
+
maxAgeMs,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
249
286
|
}
|
|
250
287
|
}
|