@aztec/validator-ha-signer 0.0.1-commit.f504929 → 0.0.1-commit.f5a9928
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 +2 -4
- 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 +70 -0
- package/dest/db/lmdb.d.ts.map +1 -0
- package/dest/db/lmdb.js +223 -0
- package/dest/db/migrations/1_initial-schema.d.ts +4 -2
- package/dest/db/migrations/1_initial-schema.d.ts.map +1 -1
- package/dest/db/migrations/1_initial-schema.js +34 -4
- package/dest/db/migrations/2_add-checkpoint-number.d.ts +7 -0
- package/dest/db/migrations/2_add-checkpoint-number.d.ts.map +1 -0
- package/dest/db/migrations/2_add-checkpoint-number.js +17 -0
- package/dest/db/postgres.d.ts +4 -2
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +15 -13
- package/dest/db/schema.d.ts +6 -6
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +9 -4
- package/dest/db/types.d.ts +44 -7
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +26 -0
- package/dest/errors.d.ts +14 -1
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +15 -0
- package/dest/factory.d.ts +38 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +95 -9
- package/dest/slashing_protection_service.d.ts +6 -4
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +14 -11
- package/dest/types.d.ts +8 -3
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +2 -1
- package/dest/validator_ha_signer.d.ts +5 -4
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +33 -12
- package/package.json +9 -7
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +308 -0
- package/src/db/migrations/1_initial-schema.ts +35 -4
- package/src/db/migrations/2_add-checkpoint-number.ts +19 -0
- package/src/db/postgres.ts +15 -11
- package/src/db/schema.ts +9 -4
- package/src/db/types.ts +63 -6
- package/src/errors.ts +21 -0
- package/src/factory.ts +130 -7
- package/src/slashing_protection_service.ts +18 -13
- package/src/types.ts +11 -0
- package/src/validator_ha_signer.ts +48 -13
|
@@ -1,21 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Initial schema for validator HA slashing protection
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Note: this migration contains a fixed snapshot of the schema at the time it was created.
|
|
5
|
+
* It must NOT import from schema.ts, which evolves over time and would cause this migration
|
|
6
|
+
* to produce different results on fresh runs vs. re-runs.
|
|
5
7
|
*/
|
|
6
8
|
import type { MigrationBuilder } from 'node-pg-migrate';
|
|
7
9
|
|
|
8
|
-
import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE
|
|
10
|
+
import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE } from '../schema.js';
|
|
11
|
+
|
|
12
|
+
// Snapshot of the initial schema — does NOT include checkpoint_number (added in migration 2).
|
|
13
|
+
const INITIAL_SCHEMA_SETUP = [
|
|
14
|
+
`CREATE TABLE IF NOT EXISTS schema_version (
|
|
15
|
+
version INTEGER PRIMARY KEY,
|
|
16
|
+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
17
|
+
);`,
|
|
18
|
+
`CREATE TABLE IF NOT EXISTS validator_duties (
|
|
19
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
20
|
+
validator_address VARCHAR(42) NOT NULL,
|
|
21
|
+
slot BIGINT NOT NULL,
|
|
22
|
+
block_number BIGINT NOT NULL,
|
|
23
|
+
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
24
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
25
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
26
|
+
message_hash VARCHAR(66) NOT NULL,
|
|
27
|
+
signature VARCHAR(132),
|
|
28
|
+
node_id VARCHAR(255) NOT NULL,
|
|
29
|
+
lock_token VARCHAR(64) NOT NULL,
|
|
30
|
+
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
31
|
+
completed_at TIMESTAMP,
|
|
32
|
+
error_message TEXT,
|
|
33
|
+
|
|
34
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
35
|
+
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
36
|
+
);`,
|
|
37
|
+
`CREATE INDEX IF NOT EXISTS idx_validator_duties_status ON validator_duties(status, started_at);`,
|
|
38
|
+
`CREATE INDEX IF NOT EXISTS idx_validator_duties_node ON validator_duties(node_id, started_at);`,
|
|
39
|
+
] as const;
|
|
9
40
|
|
|
10
41
|
export function up(pgm: MigrationBuilder): void {
|
|
11
|
-
for (const statement of
|
|
42
|
+
for (const statement of INITIAL_SCHEMA_SETUP) {
|
|
12
43
|
pgm.sql(statement);
|
|
13
44
|
}
|
|
14
45
|
|
|
15
46
|
// Insert initial schema version
|
|
16
47
|
pgm.sql(`
|
|
17
48
|
INSERT INTO schema_version (version)
|
|
18
|
-
VALUES (
|
|
49
|
+
VALUES (1)
|
|
19
50
|
ON CONFLICT (version) DO NOTHING;
|
|
20
51
|
`);
|
|
21
52
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add checkpoint_number column to validator_duties table
|
|
3
|
+
*/
|
|
4
|
+
import type { MigrationBuilder } from 'node-pg-migrate';
|
|
5
|
+
|
|
6
|
+
export function up(pgm: MigrationBuilder): void {
|
|
7
|
+
pgm.addColumn('validator_duties', {
|
|
8
|
+
// eslint-disable-next-line camelcase
|
|
9
|
+
checkpoint_number: { type: 'bigint', notNull: true, default: 0 },
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
pgm.sql(`UPDATE schema_version SET version = 2 WHERE version = 1`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function down(pgm: MigrationBuilder): void {
|
|
16
|
+
pgm.dropColumn('validator_duties', 'checkpoint_number');
|
|
17
|
+
|
|
18
|
+
pgm.sql(`UPDATE schema_version SET version = 1 WHERE version = 2`);
|
|
19
|
+
}
|
package/src/db/postgres.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
5
5
|
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
6
6
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
7
7
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
UPDATE_DUTY_SIGNED,
|
|
21
21
|
} from './schema.js';
|
|
22
22
|
import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, ValidatorDutyRecord } from './types.js';
|
|
23
|
-
import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
23
|
+
import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Minimal pool interface for database operations.
|
|
@@ -107,6 +107,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
107
107
|
params.validatorAddress.toString(),
|
|
108
108
|
params.slot.toString(),
|
|
109
109
|
params.blockNumber.toString(),
|
|
110
|
+
params.checkpointNumber.toString(),
|
|
110
111
|
blockIndexWithinCheckpoint,
|
|
111
112
|
params.dutyType,
|
|
112
113
|
params.messageHash,
|
|
@@ -220,14 +221,17 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
220
221
|
}
|
|
221
222
|
|
|
222
223
|
/**
|
|
223
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
224
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
225
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
226
|
+
* then delegates to the shared recordFromFields() converter.
|
|
224
227
|
*/
|
|
225
228
|
private rowToRecord(row: DutyRow): ValidatorDutyRecord {
|
|
226
|
-
return {
|
|
227
|
-
rollupAddress:
|
|
228
|
-
validatorAddress:
|
|
229
|
-
slot:
|
|
230
|
-
blockNumber:
|
|
229
|
+
return recordFromFields({
|
|
230
|
+
rollupAddress: row.rollup_address,
|
|
231
|
+
validatorAddress: row.validator_address,
|
|
232
|
+
slot: row.slot,
|
|
233
|
+
blockNumber: row.block_number,
|
|
234
|
+
checkpointNumber: row.checkpoint_number,
|
|
231
235
|
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
232
236
|
dutyType: row.duty_type,
|
|
233
237
|
status: row.status,
|
|
@@ -235,10 +239,10 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
235
239
|
signature: row.signature ?? undefined,
|
|
236
240
|
nodeId: row.node_id,
|
|
237
241
|
lockToken: row.lock_token,
|
|
238
|
-
|
|
239
|
-
|
|
242
|
+
startedAtMs: row.started_at.getTime(),
|
|
243
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
240
244
|
errorMessage: row.error_message ?? undefined,
|
|
241
|
-
};
|
|
245
|
+
});
|
|
242
246
|
}
|
|
243
247
|
|
|
244
248
|
/**
|
package/src/db/schema.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
/**
|
|
10
10
|
* Current schema version
|
|
11
11
|
*/
|
|
12
|
-
export const SCHEMA_VERSION =
|
|
12
|
+
export const SCHEMA_VERSION = 2;
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* SQL to create the validator_duties table
|
|
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
|
|
|
20
20
|
validator_address VARCHAR(42) NOT NULL,
|
|
21
21
|
slot BIGINT NOT NULL,
|
|
22
22
|
block_number BIGINT NOT NULL,
|
|
23
|
+
checkpoint_number BIGINT NOT NULL DEFAULT 0,
|
|
23
24
|
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
24
25
|
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
25
26
|
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
@@ -106,6 +107,7 @@ WITH inserted AS (
|
|
|
106
107
|
validator_address,
|
|
107
108
|
slot,
|
|
108
109
|
block_number,
|
|
110
|
+
checkpoint_number,
|
|
109
111
|
block_index_within_checkpoint,
|
|
110
112
|
duty_type,
|
|
111
113
|
status,
|
|
@@ -113,13 +115,14 @@ WITH inserted AS (
|
|
|
113
115
|
node_id,
|
|
114
116
|
lock_token,
|
|
115
117
|
started_at
|
|
116
|
-
) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $
|
|
118
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, 'signing', $8, $9, $10, CURRENT_TIMESTAMP)
|
|
117
119
|
ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
118
120
|
RETURNING
|
|
119
121
|
rollup_address,
|
|
120
122
|
validator_address,
|
|
121
123
|
slot,
|
|
122
124
|
block_number,
|
|
125
|
+
checkpoint_number,
|
|
123
126
|
block_index_within_checkpoint,
|
|
124
127
|
duty_type,
|
|
125
128
|
status,
|
|
@@ -139,6 +142,7 @@ SELECT
|
|
|
139
142
|
validator_address,
|
|
140
143
|
slot,
|
|
141
144
|
block_number,
|
|
145
|
+
checkpoint_number,
|
|
142
146
|
block_index_within_checkpoint,
|
|
143
147
|
duty_type,
|
|
144
148
|
status,
|
|
@@ -154,8 +158,8 @@ FROM validator_duties
|
|
|
154
158
|
WHERE rollup_address = $1
|
|
155
159
|
AND validator_address = $2
|
|
156
160
|
AND slot = $3
|
|
157
|
-
AND duty_type = $
|
|
158
|
-
AND block_index_within_checkpoint = $
|
|
161
|
+
AND duty_type = $7
|
|
162
|
+
AND block_index_within_checkpoint = $6
|
|
159
163
|
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
160
164
|
`;
|
|
161
165
|
|
|
@@ -253,6 +257,7 @@ SELECT
|
|
|
253
257
|
validator_address,
|
|
254
258
|
slot,
|
|
255
259
|
block_number,
|
|
260
|
+
checkpoint_number,
|
|
256
261
|
block_index_within_checkpoint,
|
|
257
262
|
duty_type,
|
|
258
263
|
status,
|
package/src/db/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { BlockNumber, CheckpointNumber, type IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
3
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
4
4
|
import { DutyType } from '@aztec/stdlib/ha-signing';
|
|
5
5
|
|
|
@@ -11,6 +11,7 @@ export interface DutyRow {
|
|
|
11
11
|
validator_address: string;
|
|
12
12
|
slot: string;
|
|
13
13
|
block_number: string;
|
|
14
|
+
checkpoint_number: string;
|
|
14
15
|
block_index_within_checkpoint: number;
|
|
15
16
|
duty_type: DutyType;
|
|
16
17
|
status: DutyStatus;
|
|
@@ -23,6 +24,31 @@ export interface DutyRow {
|
|
|
23
24
|
error_message: string | null;
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Plain-primitive representation of a duty record suitable for serialization
|
|
29
|
+
* (e.g. msgpackr for LMDB). All domain types are stored as their string/number
|
|
30
|
+
* equivalents. Timestamps are Unix milliseconds.
|
|
31
|
+
*/
|
|
32
|
+
export interface StoredDutyRecord {
|
|
33
|
+
rollupAddress: string;
|
|
34
|
+
validatorAddress: string;
|
|
35
|
+
slot: string;
|
|
36
|
+
blockNumber: string;
|
|
37
|
+
checkpointNumber: string;
|
|
38
|
+
blockIndexWithinCheckpoint: number;
|
|
39
|
+
dutyType: DutyType;
|
|
40
|
+
status: DutyStatus;
|
|
41
|
+
messageHash: string;
|
|
42
|
+
signature?: string;
|
|
43
|
+
nodeId: string;
|
|
44
|
+
lockToken: string;
|
|
45
|
+
/** Unix timestamp in milliseconds when signing started */
|
|
46
|
+
startedAtMs: number;
|
|
47
|
+
/** Unix timestamp in milliseconds when signing completed */
|
|
48
|
+
completedAtMs?: number;
|
|
49
|
+
errorMessage?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
26
52
|
/**
|
|
27
53
|
* Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
|
|
28
54
|
*/
|
|
@@ -42,7 +68,8 @@ export enum DutyStatus {
|
|
|
42
68
|
export { DutyType };
|
|
43
69
|
|
|
44
70
|
/**
|
|
45
|
-
*
|
|
71
|
+
* Rich representation of a validator duty, with branded types and Date objects.
|
|
72
|
+
* This is the common output type returned by all SlashingProtectionDatabase implementations.
|
|
46
73
|
*/
|
|
47
74
|
export interface ValidatorDutyRecord {
|
|
48
75
|
/** Ethereum address of the rollup contract */
|
|
@@ -51,8 +78,10 @@ export interface ValidatorDutyRecord {
|
|
|
51
78
|
validatorAddress: EthAddress;
|
|
52
79
|
/** Slot number for this duty */
|
|
53
80
|
slot: SlotNumber;
|
|
54
|
-
/** Block number for this duty */
|
|
81
|
+
/** Block number for this duty (0 for non-block-proposal duties) */
|
|
55
82
|
blockNumber: BlockNumber;
|
|
83
|
+
/** Checkpoint number for this duty (0 for attestation and vote duties) */
|
|
84
|
+
checkpointNumber: CheckpointNumber;
|
|
56
85
|
/** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
|
|
57
86
|
blockIndexWithinCheckpoint: number;
|
|
58
87
|
/** Type of duty being performed */
|
|
@@ -75,6 +104,32 @@ export interface ValidatorDutyRecord {
|
|
|
75
104
|
errorMessage?: string;
|
|
76
105
|
}
|
|
77
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Convert a {@link StoredDutyRecord} (plain-primitive wire format) to a
|
|
109
|
+
* {@link ValidatorDutyRecord} (rich domain type).
|
|
110
|
+
*
|
|
111
|
+
* Shared by LMDB and any future non-Postgres backend implementations.
|
|
112
|
+
*/
|
|
113
|
+
export function recordFromFields(stored: StoredDutyRecord): ValidatorDutyRecord {
|
|
114
|
+
return {
|
|
115
|
+
rollupAddress: EthAddress.fromString(stored.rollupAddress),
|
|
116
|
+
validatorAddress: EthAddress.fromString(stored.validatorAddress),
|
|
117
|
+
slot: SlotNumber.fromString(stored.slot),
|
|
118
|
+
blockNumber: BlockNumber.fromString(stored.blockNumber),
|
|
119
|
+
checkpointNumber: CheckpointNumber.fromString(stored.checkpointNumber),
|
|
120
|
+
blockIndexWithinCheckpoint: stored.blockIndexWithinCheckpoint,
|
|
121
|
+
dutyType: stored.dutyType,
|
|
122
|
+
status: stored.status,
|
|
123
|
+
messageHash: stored.messageHash,
|
|
124
|
+
signature: stored.signature,
|
|
125
|
+
nodeId: stored.nodeId,
|
|
126
|
+
lockToken: stored.lockToken,
|
|
127
|
+
startedAt: new Date(stored.startedAtMs),
|
|
128
|
+
completedAt: stored.completedAtMs !== undefined ? new Date(stored.completedAtMs) : undefined,
|
|
129
|
+
errorMessage: stored.errorMessage,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
78
133
|
/**
|
|
79
134
|
* Duty identifier for block proposals.
|
|
80
135
|
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
@@ -153,8 +208,10 @@ export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
|
|
|
153
208
|
* Additional parameters for checking and recording a new duty
|
|
154
209
|
*/
|
|
155
210
|
interface CheckAndRecordExtra {
|
|
156
|
-
/** Block number for this duty */
|
|
157
|
-
blockNumber: BlockNumber
|
|
211
|
+
/** Block number for this duty (0 for non-block-proposal duties) */
|
|
212
|
+
blockNumber: BlockNumber;
|
|
213
|
+
/** Checkpoint number for this duty (0 for attestation and vote duties) */
|
|
214
|
+
checkpointNumber: CheckpointNumber;
|
|
158
215
|
/** The signing root (hash) for this duty */
|
|
159
216
|
messageHash: string;
|
|
160
217
|
/** Identifier for the node that acquired the lock */
|
package/src/errors.ts
CHANGED
|
@@ -22,6 +22,27 @@ export class DutyAlreadySignedError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Thrown when the slashing-protection record for an in-flight signing operation can no longer be
|
|
27
|
+
* updated because it is no longer owned by this node - for example, the stuck-duty cleanup loop
|
|
28
|
+
* deleted the SIGNING row while the remote signer was slow. The produced signature must be
|
|
29
|
+
* discarded rather than broadcast: with no protection record in place, a later attempt for the
|
|
30
|
+
* same duty with different data would sign freely, which is slashable equivocation.
|
|
31
|
+
*/
|
|
32
|
+
export class SigningLockLostError extends Error {
|
|
33
|
+
constructor(
|
|
34
|
+
public readonly slot: SlotNumber,
|
|
35
|
+
public readonly dutyType: DutyType,
|
|
36
|
+
public readonly nodeId: string,
|
|
37
|
+
) {
|
|
38
|
+
super(
|
|
39
|
+
`Slashing protection record for ${dutyType} at slot ${slot} was lost before signing completed ` +
|
|
40
|
+
`(node ${nodeId}); discarding signature`,
|
|
41
|
+
);
|
|
42
|
+
this.name = 'SigningLockLostError';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
25
46
|
/**
|
|
26
47
|
* Thrown when attempting to sign data that conflicts with an already-signed duty.
|
|
27
48
|
* This means the same validator tried to sign DIFFERENT data for the same slot.
|
package/src/factory.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Factory functions for creating validator HA signers
|
|
3
3
|
*/
|
|
4
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
4
5
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
|
-
import
|
|
6
|
+
import { createStore } from '@aztec/kv-store/lmdb-v2';
|
|
7
|
+
import type { LocalSignerConfig, ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
|
|
6
8
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
7
9
|
|
|
8
10
|
import { Pool } from 'pg';
|
|
9
11
|
|
|
12
|
+
import { LmdbSlashingProtectionDatabase, migrateLmdbSlashingProtectionDatabase } from './db/lmdb.js';
|
|
10
13
|
import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
|
|
11
14
|
import { HASignerMetrics } from './metrics.js';
|
|
12
|
-
import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
|
|
15
|
+
import type { CreateHASignerDeps, CreateLocalSignerWithProtectionDeps, SlashingProtectionDatabase } from './types.js';
|
|
13
16
|
import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
14
17
|
|
|
15
18
|
/**
|
|
@@ -27,10 +30,9 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
|
27
30
|
* ```typescript
|
|
28
31
|
* const { signer, db } = await createHASigner({
|
|
29
32
|
* databaseUrl: process.env.DATABASE_URL,
|
|
30
|
-
* haSigningEnabled: true,
|
|
31
33
|
* nodeId: 'validator-node-1',
|
|
32
34
|
* pollingIntervalMs: 100,
|
|
33
|
-
*
|
|
35
|
+
* peerSigningTimeoutMs: 3000,
|
|
34
36
|
* });
|
|
35
37
|
* signer.start(); // Start background cleanup
|
|
36
38
|
*
|
|
@@ -56,7 +58,8 @@ export async function createHASigner(
|
|
|
56
58
|
const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } =
|
|
57
59
|
config;
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
const databaseUrlValue = databaseUrl?.getValue();
|
|
62
|
+
if (!databaseUrlValue) {
|
|
60
63
|
throw new Error('databaseUrl is required for createHASigner');
|
|
61
64
|
}
|
|
62
65
|
|
|
@@ -67,7 +70,7 @@ export async function createHASigner(
|
|
|
67
70
|
let pool: Pool;
|
|
68
71
|
if (!deps?.pool) {
|
|
69
72
|
pool = new Pool({
|
|
70
|
-
connectionString:
|
|
73
|
+
connectionString: databaseUrlValue,
|
|
71
74
|
max: poolMaxCount ?? 10,
|
|
72
75
|
min: poolMinCount ?? 0,
|
|
73
76
|
idleTimeoutMillis: poolIdleTimeoutMs ?? 10_000,
|
|
@@ -77,6 +80,16 @@ export async function createHASigner(
|
|
|
77
80
|
pool = deps.pool;
|
|
78
81
|
}
|
|
79
82
|
|
|
83
|
+
// pg re-emits idle-client errors (e.g. a Postgres restart severing an idle connection) on the
|
|
84
|
+
// pool. Without an 'error' listener, Node escalates these to an uncaughtException and crashes the
|
|
85
|
+
// process - taking down every HA replica sharing the DB at once. pg destroys and replaces the
|
|
86
|
+
// errored client itself, so logging is the only action needed. Log just message/code, never the
|
|
87
|
+
// raw error object (it can carry connection metadata).
|
|
88
|
+
const log = createLogger('validator-ha-signer:factory');
|
|
89
|
+
pool.on('error', (err: NodeJS.ErrnoException) => {
|
|
90
|
+
log.warn('Postgres pool error on idle client', { message: err.message, code: err.code });
|
|
91
|
+
});
|
|
92
|
+
|
|
80
93
|
// Create database instance
|
|
81
94
|
const db = new PostgresSlashingProtectionDatabase(pool);
|
|
82
95
|
|
|
@@ -87,7 +100,117 @@ export async function createHASigner(
|
|
|
87
100
|
const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
|
|
88
101
|
|
|
89
102
|
// Create signer
|
|
90
|
-
const signer = new ValidatorHASigner(db,
|
|
103
|
+
const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
|
|
104
|
+
|
|
105
|
+
return { signer, db };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Create a local (single-node) signing protection signer backed by LMDB.
|
|
110
|
+
*
|
|
111
|
+
* This provides double-signing protection for nodes that are NOT running in a
|
|
112
|
+
* high-availability (multi-node) setup. It prevents a proposer from sending two
|
|
113
|
+
* proposals for the same slot if the node crashes and restarts mid-proposal.
|
|
114
|
+
*
|
|
115
|
+
* `config.dataDirectory` is required so the protection database is persisted to disk and survives
|
|
116
|
+
* crashes/restarts. Booting without it throws, since an ephemeral store silently drops all
|
|
117
|
+
* double-signing protection across restarts. Set `config.allowEphemeralSigningProtection` to opt
|
|
118
|
+
* into the ephemeral store anyway (dev/test networks only) — a loud warning is logged in that case.
|
|
119
|
+
*
|
|
120
|
+
* @param config - Local signer config
|
|
121
|
+
* @param deps - Optional dependencies (telemetry, date provider).
|
|
122
|
+
* @returns An object containing the signer and database instances.
|
|
123
|
+
*/
|
|
124
|
+
export async function createLocalSignerWithProtection(
|
|
125
|
+
config: LocalSignerConfig,
|
|
126
|
+
deps?: CreateLocalSignerWithProtectionDeps,
|
|
127
|
+
): Promise<{
|
|
128
|
+
signer: ValidatorHASigner;
|
|
129
|
+
db: SlashingProtectionDatabase;
|
|
130
|
+
}> {
|
|
131
|
+
const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
|
|
132
|
+
const dateProvider = deps?.dateProvider ?? new DateProvider();
|
|
91
133
|
|
|
134
|
+
const log = createLogger('validator-ha-signer:factory');
|
|
135
|
+
|
|
136
|
+
if (!config.dataDirectory) {
|
|
137
|
+
if (!config.allowEphemeralSigningProtection) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
'Local signing protection requires a persistent data directory, but none was configured. ' +
|
|
140
|
+
'Set DATA_DIRECTORY so double-signing protection survives restarts, or explicitly opt into an ' +
|
|
141
|
+
'ephemeral store (dev/test only) with VALIDATOR_ALLOW_EPHEMERAL_SIGNING_PROTECTION=true.',
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
log.warn(
|
|
145
|
+
'Local signing protection is running with an EPHEMERAL store: no data directory is configured. ' +
|
|
146
|
+
'Double-signing protection will NOT survive a restart. This is unsafe for production validators.',
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const kvStore = await createStore(
|
|
151
|
+
'signing-protection',
|
|
152
|
+
LmdbSlashingProtectionDatabase.SCHEMA_VERSION,
|
|
153
|
+
{
|
|
154
|
+
dataDirectory: config.dataDirectory,
|
|
155
|
+
dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
|
|
156
|
+
rollupAddress: config.rollupAddress,
|
|
157
|
+
},
|
|
158
|
+
undefined,
|
|
159
|
+
{
|
|
160
|
+
onUpgrade: (dataDirectory, currentVersion, latestVersion) =>
|
|
161
|
+
migrateLmdbSlashingProtectionDatabase(
|
|
162
|
+
dataDirectory,
|
|
163
|
+
currentVersion,
|
|
164
|
+
latestVersion,
|
|
165
|
+
config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
|
|
166
|
+
),
|
|
167
|
+
schemaVersionMismatchPolicy: 'throw',
|
|
168
|
+
versionFileReadFailurePolicy: 'throw',
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
|
|
173
|
+
|
|
174
|
+
const signerConfig = {
|
|
175
|
+
...config,
|
|
176
|
+
nodeId: config.nodeId || 'local',
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
|
|
180
|
+
|
|
181
|
+
const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
|
|
182
|
+
|
|
183
|
+
return { signer, db };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
|
|
188
|
+
* multiple validator nodes in the same process. Used for testing HA setups.
|
|
189
|
+
*/
|
|
190
|
+
export async function createSharedSlashingProtectionDb(
|
|
191
|
+
dateProvider: DateProvider = new DateProvider(),
|
|
192
|
+
): Promise<SlashingProtectionDatabase> {
|
|
193
|
+
const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
|
|
194
|
+
dataStoreMapSizeKb: 1024 * 1024,
|
|
195
|
+
});
|
|
196
|
+
return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
|
|
201
|
+
* Used for testing HA setups where multiple nodes share the same protection database.
|
|
202
|
+
*/
|
|
203
|
+
export function createSignerFromSharedDb(
|
|
204
|
+
db: SlashingProtectionDatabase,
|
|
205
|
+
config: Pick<
|
|
206
|
+
ValidatorHASignerConfig,
|
|
207
|
+
'nodeId' | 'pollingIntervalMs' | 'peerSigningTimeoutMs' | 'maxStuckDutiesAgeMs' | 'rollupAddress'
|
|
208
|
+
>,
|
|
209
|
+
deps?: CreateLocalSignerWithProtectionDeps,
|
|
210
|
+
): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
|
|
211
|
+
const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
|
|
212
|
+
const dateProvider = deps?.dateProvider ?? new DateProvider();
|
|
213
|
+
const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
|
|
214
|
+
const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
|
|
92
215
|
return { signer, db };
|
|
93
216
|
}
|
|
@@ -8,7 +8,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
10
|
import type { DateProvider } from '@aztec/foundation/timer';
|
|
11
|
-
import type {
|
|
11
|
+
import type { BaseSignerConfig } from '@aztec/stdlib/ha-signing';
|
|
12
12
|
|
|
13
13
|
import {
|
|
14
14
|
type CheckAndRecordParams,
|
|
@@ -26,6 +26,9 @@ export interface SlashingProtectionServiceDeps {
|
|
|
26
26
|
dateProvider: DateProvider;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/** Default max age (ms) of a stuck SIGNING duty before cleanup reclaims it: 2x the 72s Aztec slot duration. */
|
|
30
|
+
export const DEFAULT_MAX_STUCK_DUTIES_AGE_MS = 144_000;
|
|
31
|
+
|
|
29
32
|
/**
|
|
30
33
|
* Slashing Protection Service
|
|
31
34
|
*
|
|
@@ -44,7 +47,7 @@ export interface SlashingProtectionServiceDeps {
|
|
|
44
47
|
export class SlashingProtectionService {
|
|
45
48
|
private readonly log: Logger;
|
|
46
49
|
private readonly pollingIntervalMs: number;
|
|
47
|
-
private readonly
|
|
50
|
+
private readonly peerSigningTimeoutMs: number;
|
|
48
51
|
private readonly maxStuckDutiesAgeMs: number;
|
|
49
52
|
|
|
50
53
|
private readonly metrics: HASignerMetrics;
|
|
@@ -55,14 +58,13 @@ export class SlashingProtectionService {
|
|
|
55
58
|
|
|
56
59
|
constructor(
|
|
57
60
|
private readonly db: SlashingProtectionDatabase,
|
|
58
|
-
private readonly config:
|
|
61
|
+
private readonly config: BaseSignerConfig,
|
|
59
62
|
deps: SlashingProtectionServiceDeps,
|
|
60
63
|
) {
|
|
61
64
|
this.log = createLogger('slashing-protection');
|
|
62
65
|
this.pollingIntervalMs = config.pollingIntervalMs;
|
|
63
|
-
this.
|
|
64
|
-
|
|
65
|
-
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
66
|
+
this.peerSigningTimeoutMs = config.peerSigningTimeoutMs;
|
|
67
|
+
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? DEFAULT_MAX_STUCK_DUTIES_AGE_MS;
|
|
66
68
|
|
|
67
69
|
this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
|
|
68
70
|
this.metrics = deps.metrics;
|
|
@@ -99,7 +101,7 @@ export class SlashingProtectionService {
|
|
|
99
101
|
|
|
100
102
|
if (isNew) {
|
|
101
103
|
// We successfully acquired the lock
|
|
102
|
-
this.log.
|
|
104
|
+
this.log.verbose(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
|
|
103
105
|
validatorAddress: validatorAddress.toString(),
|
|
104
106
|
nodeId,
|
|
105
107
|
});
|
|
@@ -132,10 +134,10 @@ export class SlashingProtectionService {
|
|
|
132
134
|
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
|
|
133
135
|
} else if (record.status === DutyStatus.SIGNING) {
|
|
134
136
|
// Another node is currently signing - check for timeout
|
|
135
|
-
if (this.dateProvider.now() - startTime > this.
|
|
137
|
+
if (this.dateProvider.now() - startTime > this.peerSigningTimeoutMs) {
|
|
136
138
|
this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
|
|
137
139
|
validatorAddress: validatorAddress.toString(),
|
|
138
|
-
timeoutMs: this.
|
|
140
|
+
timeoutMs: this.peerSigningTimeoutMs,
|
|
139
141
|
signingNodeId: record.nodeId,
|
|
140
142
|
});
|
|
141
143
|
this.metrics.recordDutyAlreadySigned(dutyType);
|
|
@@ -177,7 +179,7 @@ export class SlashingProtectionService {
|
|
|
177
179
|
);
|
|
178
180
|
|
|
179
181
|
if (success) {
|
|
180
|
-
this.log.
|
|
182
|
+
this.log.verbose(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
|
|
181
183
|
validatorAddress: validatorAddress.toString(),
|
|
182
184
|
nodeId,
|
|
183
185
|
});
|
|
@@ -241,10 +243,10 @@ export class SlashingProtectionService {
|
|
|
241
243
|
*/
|
|
242
244
|
async start() {
|
|
243
245
|
// One-time cleanup at startup: remove duties from previous rollup versions
|
|
244
|
-
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.
|
|
246
|
+
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.rollupAddress);
|
|
245
247
|
if (numOutdatedRollupDuties > 0) {
|
|
246
248
|
this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
|
|
247
|
-
currentRollupAddress: this.config.
|
|
249
|
+
currentRollupAddress: this.config.rollupAddress.toString(),
|
|
248
250
|
});
|
|
249
251
|
this.metrics.recordCleanup('outdated_rollup', numOutdatedRollupDuties);
|
|
250
252
|
}
|
|
@@ -275,7 +277,10 @@ export class SlashingProtectionService {
|
|
|
275
277
|
* Runs in the background via RunningPromise.
|
|
276
278
|
*/
|
|
277
279
|
private async cleanup() {
|
|
278
|
-
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status)
|
|
280
|
+
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status).
|
|
281
|
+
// This cannot race an in-flight signing: every signing operation is hard-bounded by a timeout
|
|
282
|
+
// clamped below maxStuckDutiesAgeMs / 2 (see ValidatorHASigner), so a live SIGNING row is
|
|
283
|
+
// always released long before it can be considered stuck.
|
|
279
284
|
const numStuckDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
|
|
280
285
|
if (numStuckDuties > 0) {
|
|
281
286
|
this.log.verbose(`Cleaned up ${numStuckDuties} stuck duties`, {
|