@aztec/validator-ha-signer 0.0.1-commit.1142ef1 → 0.0.1-commit.11bf3dd6e
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 +50 -37
- 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 +189 -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 +37 -6
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +88 -28
- package/dest/db/schema.d.ts +22 -11
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +55 -21
- package/dest/db/types.d.ts +116 -34
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +58 -8
- package/dest/errors.d.ts +9 -5
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +7 -4
- package/dest/factory.d.ts +42 -15
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +80 -15
- package/dest/metrics.d.ts +51 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +103 -0
- package/dest/migrations.d.ts +1 -1
- package/dest/migrations.d.ts.map +1 -1
- package/dest/migrations.js +13 -2
- package/dest/slashing_protection_service.d.ts +25 -6
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +74 -22
- package/dest/test/pglite_pool.d.ts +92 -0
- package/dest/test/pglite_pool.d.ts.map +1 -0
- package/dest/test/pglite_pool.js +210 -0
- package/dest/types.d.ts +41 -16
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +5 -1
- package/dest/validator_ha_signer.d.ts +18 -13
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +47 -36
- package/package.json +15 -10
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +265 -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 +111 -27
- package/src/db/schema.ts +57 -21
- package/src/db/types.ts +169 -33
- package/src/errors.ts +7 -2
- package/src/factory.ts +99 -15
- package/src/metrics.ts +138 -0
- package/src/migrations.ts +17 -1
- package/src/slashing_protection_service.ts +119 -27
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +71 -16
- package/src/validator_ha_signer.ts +67 -45
- package/dest/config.d.ts +0 -47
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -64
- package/src/config.ts +0 -116
package/src/db/schema.ts
CHANGED
|
@@ -9,18 +9,21 @@
|
|
|
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
|
|
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
|
-
|
|
23
|
+
checkpoint_number BIGINT NOT NULL DEFAULT 0,
|
|
24
|
+
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
25
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
26
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
24
27
|
message_hash VARCHAR(66) NOT NULL,
|
|
25
28
|
signature VARCHAR(132),
|
|
26
29
|
node_id VARCHAR(255) NOT NULL,
|
|
@@ -29,7 +32,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
|
|
|
29
32
|
completed_at TIMESTAMP,
|
|
30
33
|
error_message TEXT,
|
|
31
34
|
|
|
32
|
-
PRIMARY KEY (validator_address, slot, duty_type),
|
|
35
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
33
36
|
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
34
37
|
);
|
|
35
38
|
`;
|
|
@@ -92,25 +95,35 @@ SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
|
92
95
|
* returns the existing record instead.
|
|
93
96
|
*
|
|
94
97
|
* Returns the record with an `is_new` flag indicating whether we inserted or got existing.
|
|
98
|
+
*
|
|
99
|
+
* Note: In high concurrency scenarios, if the INSERT conflicts and another transaction
|
|
100
|
+
* just committed the row, there's a small window where the SELECT might not see it yet.
|
|
101
|
+
* The application layer should retry if no rows are returned.
|
|
95
102
|
*/
|
|
96
103
|
export const INSERT_OR_GET_DUTY = `
|
|
97
104
|
WITH inserted AS (
|
|
98
105
|
INSERT INTO validator_duties (
|
|
106
|
+
rollup_address,
|
|
99
107
|
validator_address,
|
|
100
108
|
slot,
|
|
101
109
|
block_number,
|
|
110
|
+
checkpoint_number,
|
|
111
|
+
block_index_within_checkpoint,
|
|
102
112
|
duty_type,
|
|
103
113
|
status,
|
|
104
114
|
message_hash,
|
|
105
115
|
node_id,
|
|
106
116
|
lock_token,
|
|
107
117
|
started_at
|
|
108
|
-
) VALUES ($1, $2, $3, $4, 'signing', $
|
|
109
|
-
ON CONFLICT (validator_address, slot, duty_type) DO NOTHING
|
|
118
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, 'signing', $8, $9, $10, CURRENT_TIMESTAMP)
|
|
119
|
+
ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
110
120
|
RETURNING
|
|
121
|
+
rollup_address,
|
|
111
122
|
validator_address,
|
|
112
123
|
slot,
|
|
113
124
|
block_number,
|
|
125
|
+
checkpoint_number,
|
|
126
|
+
block_index_within_checkpoint,
|
|
114
127
|
duty_type,
|
|
115
128
|
status,
|
|
116
129
|
message_hash,
|
|
@@ -125,9 +138,12 @@ WITH inserted AS (
|
|
|
125
138
|
SELECT * FROM inserted
|
|
126
139
|
UNION ALL
|
|
127
140
|
SELECT
|
|
141
|
+
rollup_address,
|
|
128
142
|
validator_address,
|
|
129
143
|
slot,
|
|
130
144
|
block_number,
|
|
145
|
+
checkpoint_number,
|
|
146
|
+
block_index_within_checkpoint,
|
|
131
147
|
duty_type,
|
|
132
148
|
status,
|
|
133
149
|
message_hash,
|
|
@@ -139,9 +155,11 @@ SELECT
|
|
|
139
155
|
error_message,
|
|
140
156
|
FALSE as is_new
|
|
141
157
|
FROM validator_duties
|
|
142
|
-
WHERE
|
|
143
|
-
AND
|
|
144
|
-
AND
|
|
158
|
+
WHERE rollup_address = $1
|
|
159
|
+
AND validator_address = $2
|
|
160
|
+
AND slot = $3
|
|
161
|
+
AND duty_type = $7
|
|
162
|
+
AND block_index_within_checkpoint = $6
|
|
145
163
|
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
146
164
|
`;
|
|
147
165
|
|
|
@@ -153,11 +171,13 @@ UPDATE validator_duties
|
|
|
153
171
|
SET status = 'signed',
|
|
154
172
|
signature = $1,
|
|
155
173
|
completed_at = CURRENT_TIMESTAMP
|
|
156
|
-
WHERE
|
|
157
|
-
AND
|
|
158
|
-
AND
|
|
174
|
+
WHERE rollup_address = $2
|
|
175
|
+
AND validator_address = $3
|
|
176
|
+
AND slot = $4
|
|
177
|
+
AND duty_type = $5
|
|
178
|
+
AND block_index_within_checkpoint = $6
|
|
159
179
|
AND status = 'signing'
|
|
160
|
-
AND lock_token = $
|
|
180
|
+
AND lock_token = $7;
|
|
161
181
|
`;
|
|
162
182
|
|
|
163
183
|
/**
|
|
@@ -166,11 +186,13 @@ WHERE validator_address = $2
|
|
|
166
186
|
*/
|
|
167
187
|
export const DELETE_DUTY = `
|
|
168
188
|
DELETE FROM validator_duties
|
|
169
|
-
WHERE
|
|
170
|
-
AND
|
|
171
|
-
AND
|
|
189
|
+
WHERE rollup_address = $1
|
|
190
|
+
AND validator_address = $2
|
|
191
|
+
AND slot = $3
|
|
192
|
+
AND duty_type = $4
|
|
193
|
+
AND block_index_within_checkpoint = $5
|
|
172
194
|
AND status = 'signing'
|
|
173
|
-
AND lock_token = $
|
|
195
|
+
AND lock_token = $6;
|
|
174
196
|
`;
|
|
175
197
|
|
|
176
198
|
/**
|
|
@@ -185,23 +207,34 @@ WHERE status = 'signed'
|
|
|
185
207
|
|
|
186
208
|
/**
|
|
187
209
|
* Query to clean up old duties (for maintenance)
|
|
188
|
-
* Removes duties older than a specified
|
|
210
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
189
211
|
*/
|
|
190
212
|
export const CLEANUP_OLD_DUTIES = `
|
|
191
213
|
DELETE FROM validator_duties
|
|
192
|
-
WHERE status
|
|
193
|
-
AND started_at < $1;
|
|
214
|
+
WHERE status = 'signed'
|
|
215
|
+
AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
|
|
194
216
|
`;
|
|
195
217
|
|
|
196
218
|
/**
|
|
197
219
|
* Query to cleanup own stuck duties
|
|
198
220
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
221
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
199
222
|
*/
|
|
200
223
|
export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
201
224
|
DELETE FROM validator_duties
|
|
202
225
|
WHERE node_id = $1
|
|
203
226
|
AND status = 'signing'
|
|
204
|
-
AND started_at < $2;
|
|
227
|
+
AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
|
|
228
|
+
`;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Query to cleanup duties with outdated rollup address
|
|
232
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
233
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
234
|
+
*/
|
|
235
|
+
export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
|
|
236
|
+
DELETE FROM validator_duties
|
|
237
|
+
WHERE rollup_address != $1;
|
|
205
238
|
`;
|
|
206
239
|
|
|
207
240
|
/**
|
|
@@ -220,9 +253,12 @@ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
|
|
|
220
253
|
*/
|
|
221
254
|
export const GET_STUCK_DUTIES = `
|
|
222
255
|
SELECT
|
|
256
|
+
rollup_address,
|
|
223
257
|
validator_address,
|
|
224
258
|
slot,
|
|
225
259
|
block_number,
|
|
260
|
+
checkpoint_number,
|
|
261
|
+
block_index_within_checkpoint,
|
|
226
262
|
duty_type,
|
|
227
263
|
status,
|
|
228
264
|
message_hash,
|
package/src/db/types.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { BlockNumber, CheckpointNumber, type IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
3
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
4
|
+
import { DutyType } from '@aztec/stdlib/ha-signing';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Row type from PostgreSQL query
|
|
6
8
|
*/
|
|
7
9
|
export interface DutyRow {
|
|
10
|
+
rollup_address: string;
|
|
8
11
|
validator_address: string;
|
|
9
12
|
slot: string;
|
|
10
13
|
block_number: string;
|
|
14
|
+
checkpoint_number: string;
|
|
15
|
+
block_index_within_checkpoint: number;
|
|
11
16
|
duty_type: DutyType;
|
|
12
17
|
status: DutyStatus;
|
|
13
18
|
message_hash: string;
|
|
@@ -20,19 +25,35 @@ export interface DutyRow {
|
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/**
|
|
23
|
-
*
|
|
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.
|
|
24
31
|
*/
|
|
25
|
-
export interface
|
|
26
|
-
|
|
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;
|
|
27
50
|
}
|
|
28
51
|
|
|
29
52
|
/**
|
|
30
|
-
*
|
|
53
|
+
* Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
|
|
31
54
|
*/
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
ATTESTATION = 'ATTESTATION',
|
|
35
|
-
ATTESTATIONS_AND_SIGNERS = 'ATTESTATIONS_AND_SIGNERS',
|
|
55
|
+
export interface InsertOrGetRow extends DutyRow {
|
|
56
|
+
is_new: boolean;
|
|
36
57
|
}
|
|
37
58
|
|
|
38
59
|
/**
|
|
@@ -43,16 +64,26 @@ export enum DutyStatus {
|
|
|
43
64
|
SIGNED = 'signed',
|
|
44
65
|
}
|
|
45
66
|
|
|
67
|
+
// Re-export DutyType from stdlib
|
|
68
|
+
export { DutyType };
|
|
69
|
+
|
|
46
70
|
/**
|
|
47
|
-
*
|
|
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.
|
|
48
73
|
*/
|
|
49
74
|
export interface ValidatorDutyRecord {
|
|
75
|
+
/** Ethereum address of the rollup contract */
|
|
76
|
+
rollupAddress: EthAddress;
|
|
50
77
|
/** Ethereum address of the validator */
|
|
51
78
|
validatorAddress: EthAddress;
|
|
52
79
|
/** Slot number for this duty */
|
|
53
|
-
slot:
|
|
54
|
-
/** Block number for this duty */
|
|
55
|
-
blockNumber:
|
|
80
|
+
slot: SlotNumber;
|
|
81
|
+
/** Block number for this duty (0 for non-block-proposal duties) */
|
|
82
|
+
blockNumber: BlockNumber;
|
|
83
|
+
/** Checkpoint number for this duty (0 for attestation and vote duties) */
|
|
84
|
+
checkpointNumber: CheckpointNumber;
|
|
85
|
+
/** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
|
|
86
|
+
blockIndexWithinCheckpoint: number;
|
|
56
87
|
/** Type of duty being performed */
|
|
57
88
|
dutyType: DutyType;
|
|
58
89
|
/** Current status of the duty */
|
|
@@ -69,49 +100,154 @@ export interface ValidatorDutyRecord {
|
|
|
69
100
|
startedAt: Date;
|
|
70
101
|
/** When the duty signing was completed (success or failure) */
|
|
71
102
|
completedAt?: Date;
|
|
72
|
-
/** Error message
|
|
103
|
+
/** Error message (currently unused) */
|
|
73
104
|
errorMessage?: string;
|
|
74
105
|
}
|
|
75
106
|
|
|
76
107
|
/**
|
|
77
|
-
*
|
|
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
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Duty identifier for block proposals.
|
|
135
|
+
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
78
136
|
*/
|
|
79
|
-
export interface
|
|
137
|
+
export interface BlockProposalDutyIdentifier {
|
|
138
|
+
rollupAddress: EthAddress;
|
|
80
139
|
validatorAddress: EthAddress;
|
|
81
|
-
slot:
|
|
82
|
-
|
|
140
|
+
slot: SlotNumber;
|
|
141
|
+
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
142
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint;
|
|
143
|
+
dutyType: DutyType.BLOCK_PROPOSAL;
|
|
83
144
|
}
|
|
84
145
|
|
|
85
146
|
/**
|
|
86
|
-
*
|
|
147
|
+
* Duty identifier for non-block-proposal duties.
|
|
148
|
+
* blockIndexWithinCheckpoint is not applicable (internally stored as -1).
|
|
87
149
|
*/
|
|
88
|
-
export interface
|
|
150
|
+
export interface OtherDutyIdentifier {
|
|
151
|
+
rollupAddress: EthAddress;
|
|
89
152
|
validatorAddress: EthAddress;
|
|
90
|
-
slot:
|
|
91
|
-
|
|
92
|
-
|
|
153
|
+
slot: SlotNumber;
|
|
154
|
+
dutyType:
|
|
155
|
+
| DutyType.CHECKPOINT_PROPOSAL
|
|
156
|
+
| DutyType.ATTESTATION
|
|
157
|
+
| DutyType.ATTESTATIONS_AND_SIGNERS
|
|
158
|
+
| DutyType.GOVERNANCE_VOTE
|
|
159
|
+
| DutyType.SLASHING_VOTE
|
|
160
|
+
| DutyType.AUTH_REQUEST
|
|
161
|
+
| DutyType.TXS;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Minimal info needed to identify a unique duty.
|
|
166
|
+
* Uses discriminated union to enforce type safety:
|
|
167
|
+
* - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
|
|
168
|
+
* - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
|
|
169
|
+
*/
|
|
170
|
+
export type DutyIdentifier = BlockProposalDutyIdentifier | OtherDutyIdentifier;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Validates and normalizes the block index for a duty.
|
|
174
|
+
* - BLOCK_PROPOSAL: validates blockIndexWithinCheckpoint is provided and >= 0
|
|
175
|
+
* - Other duty types: always returns -1
|
|
176
|
+
*
|
|
177
|
+
* @throws Error if BLOCK_PROPOSAL is missing blockIndexWithinCheckpoint or has invalid value
|
|
178
|
+
*/
|
|
179
|
+
export function normalizeBlockIndex(dutyType: DutyType, blockIndexWithinCheckpoint: number | undefined): number {
|
|
180
|
+
if (dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
181
|
+
if (blockIndexWithinCheckpoint === undefined) {
|
|
182
|
+
throw new Error('BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint to be specified');
|
|
183
|
+
}
|
|
184
|
+
if (blockIndexWithinCheckpoint < 0) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint >= 0, got ${blockIndexWithinCheckpoint}`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
return blockIndexWithinCheckpoint;
|
|
190
|
+
}
|
|
191
|
+
// For all other duty types, always use -1
|
|
192
|
+
return -1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Gets the block index from a DutyIdentifier.
|
|
197
|
+
* - BLOCK_PROPOSAL: returns the blockIndexWithinCheckpoint
|
|
198
|
+
* - Other duty types: returns -1
|
|
199
|
+
*/
|
|
200
|
+
export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
|
|
201
|
+
if (duty.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
202
|
+
return duty.blockIndexWithinCheckpoint;
|
|
203
|
+
}
|
|
204
|
+
return -1;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Additional parameters for checking and recording a new duty
|
|
209
|
+
*/
|
|
210
|
+
interface CheckAndRecordExtra {
|
|
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;
|
|
215
|
+
/** The signing root (hash) for this duty */
|
|
93
216
|
messageHash: string;
|
|
217
|
+
/** Identifier for the node that acquired the lock */
|
|
94
218
|
nodeId: string;
|
|
95
219
|
}
|
|
96
220
|
|
|
97
221
|
/**
|
|
98
|
-
* Parameters for recording a
|
|
222
|
+
* Parameters for checking and recording a new duty.
|
|
223
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
99
224
|
*/
|
|
100
|
-
export
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
225
|
+
export type CheckAndRecordParams = DutyIdentifier & CheckAndRecordExtra;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Additional parameters for recording a successful signing
|
|
229
|
+
*/
|
|
230
|
+
interface RecordSuccessExtra {
|
|
104
231
|
signature: Signature;
|
|
105
232
|
nodeId: string;
|
|
106
233
|
lockToken: string;
|
|
107
234
|
}
|
|
108
235
|
|
|
109
236
|
/**
|
|
110
|
-
* Parameters for
|
|
237
|
+
* Parameters for recording a successful signing.
|
|
238
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
111
239
|
*/
|
|
112
|
-
export
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
240
|
+
export type RecordSuccessParams = DutyIdentifier & RecordSuccessExtra;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Additional parameters for deleting a duty
|
|
244
|
+
*/
|
|
245
|
+
interface DeleteDutyExtra {
|
|
116
246
|
lockToken: string;
|
|
117
247
|
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Parameters for deleting a duty.
|
|
251
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
252
|
+
*/
|
|
253
|
+
export type DeleteDutyParams = DutyIdentifier & DeleteDutyExtra;
|
package/src/errors.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Custom errors for the validator HA signer
|
|
3
3
|
*/
|
|
4
|
+
import type { SlotNumber } from '@aztec/foundation/branded-types';
|
|
5
|
+
|
|
4
6
|
import type { DutyType } from './db/types.js';
|
|
5
7
|
|
|
6
8
|
/**
|
|
@@ -10,8 +12,9 @@ import type { DutyType } from './db/types.js';
|
|
|
10
12
|
*/
|
|
11
13
|
export class DutyAlreadySignedError extends Error {
|
|
12
14
|
constructor(
|
|
13
|
-
public readonly slot:
|
|
15
|
+
public readonly slot: SlotNumber,
|
|
14
16
|
public readonly dutyType: DutyType,
|
|
17
|
+
public readonly blockIndexWithinCheckpoint: number,
|
|
15
18
|
public readonly signedByNode: string,
|
|
16
19
|
) {
|
|
17
20
|
super(`Duty ${dutyType} for slot ${slot} already signed by node ${signedByNode}`);
|
|
@@ -28,10 +31,12 @@ export class DutyAlreadySignedError extends Error {
|
|
|
28
31
|
*/
|
|
29
32
|
export class SlashingProtectionError extends Error {
|
|
30
33
|
constructor(
|
|
31
|
-
public readonly slot:
|
|
34
|
+
public readonly slot: SlotNumber,
|
|
32
35
|
public readonly dutyType: DutyType,
|
|
36
|
+
public readonly blockIndexWithinCheckpoint: number,
|
|
33
37
|
public readonly existingMessageHash: string,
|
|
34
38
|
public readonly attemptedMessageHash: string,
|
|
39
|
+
public readonly signedByNode: string,
|
|
35
40
|
) {
|
|
36
41
|
super(
|
|
37
42
|
`Slashing protection: ${dutyType} for slot ${slot} was already signed with different data. ` +
|
package/src/factory.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Factory functions for creating validator HA signers
|
|
3
3
|
*/
|
|
4
|
+
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
|
+
import { createStore } from '@aztec/kv-store/lmdb-v2';
|
|
6
|
+
import type { LocalSignerConfig, ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
|
|
7
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
8
|
+
|
|
4
9
|
import { Pool } from 'pg';
|
|
5
10
|
|
|
6
|
-
import
|
|
11
|
+
import { LmdbSlashingProtectionDatabase } from './db/lmdb.js';
|
|
7
12
|
import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
|
|
8
|
-
import
|
|
13
|
+
import { HASignerMetrics } from './metrics.js';
|
|
14
|
+
import type { CreateHASignerDeps, CreateLocalSignerWithProtectionDeps, SlashingProtectionDatabase } from './types.js';
|
|
9
15
|
import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
10
16
|
|
|
11
17
|
/**
|
|
@@ -23,7 +29,6 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
|
23
29
|
* ```typescript
|
|
24
30
|
* const { signer, db } = await createHASigner({
|
|
25
31
|
* databaseUrl: process.env.DATABASE_URL,
|
|
26
|
-
* enabled: true,
|
|
27
32
|
* nodeId: 'validator-node-1',
|
|
28
33
|
* pollingIntervalMs: 100,
|
|
29
34
|
* signingTimeoutMs: 3000,
|
|
@@ -35,23 +40,15 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
|
35
40
|
* await signer.stop(); // On shutdown
|
|
36
41
|
* ```
|
|
37
42
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* const { signer, db } = await createHASigner({
|
|
41
|
-
* databaseUrl: process.env.DATABASE_URL,
|
|
42
|
-
* enabled: true,
|
|
43
|
-
* nodeId: 'validator-node-1',
|
|
44
|
-
* runMigrations: true, // Auto-run migrations on startup
|
|
45
|
-
* });
|
|
46
|
-
* signer.start();
|
|
47
|
-
* ```
|
|
43
|
+
* Note: Migrations must be run separately using `aztec migrate-ha-db up` before
|
|
44
|
+
* creating the signer. The factory will verify the schema is initialized via `db.initialize()`.
|
|
48
45
|
*
|
|
49
46
|
* @param config - Configuration for the HA signer
|
|
50
47
|
* @param deps - Optional dependencies (e.g., for testing)
|
|
51
48
|
* @returns An object containing the signer and database instances
|
|
52
49
|
*/
|
|
53
50
|
export async function createHASigner(
|
|
54
|
-
config:
|
|
51
|
+
config: ValidatorHASignerConfig,
|
|
55
52
|
deps?: CreateHASignerDeps,
|
|
56
53
|
): Promise<{
|
|
57
54
|
signer: ValidatorHASigner;
|
|
@@ -60,6 +57,13 @@ export async function createHASigner(
|
|
|
60
57
|
const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } =
|
|
61
58
|
config;
|
|
62
59
|
|
|
60
|
+
if (!databaseUrl) {
|
|
61
|
+
throw new Error('databaseUrl is required for createHASigner');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
|
|
65
|
+
const dateProvider = deps?.dateProvider ?? new DateProvider();
|
|
66
|
+
|
|
63
67
|
// Create connection pool (or use provided pool)
|
|
64
68
|
let pool: Pool;
|
|
65
69
|
if (!deps?.pool) {
|
|
@@ -80,8 +84,88 @@ export async function createHASigner(
|
|
|
80
84
|
// Verify database schema is initialized and version matches
|
|
81
85
|
await db.initialize();
|
|
82
86
|
|
|
87
|
+
// Create metrics
|
|
88
|
+
const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
|
|
89
|
+
|
|
83
90
|
// Create signer
|
|
84
|
-
const signer = new ValidatorHASigner(db, {
|
|
91
|
+
const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
|
|
92
|
+
|
|
93
|
+
return { signer, db };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Create a local (single-node) signing protection signer backed by LMDB.
|
|
98
|
+
*
|
|
99
|
+
* This provides double-signing protection for nodes that are NOT running in a
|
|
100
|
+
* high-availability (multi-node) setup. It prevents a proposer from sending two
|
|
101
|
+
* proposals for the same slot if the node crashes and restarts mid-proposal.
|
|
102
|
+
*
|
|
103
|
+
* When `config.dataDirectory` is set, the protection database is persisted to disk
|
|
104
|
+
* and survives crashes/restarts. When unset, an ephemeral in-memory store is
|
|
105
|
+
* used which protects within a single run but not across restarts.
|
|
106
|
+
*
|
|
107
|
+
* @param config - Local signer config
|
|
108
|
+
* @param deps - Optional dependencies (telemetry, date provider).
|
|
109
|
+
* @returns An object containing the signer and database instances.
|
|
110
|
+
*/
|
|
111
|
+
export async function createLocalSignerWithProtection(
|
|
112
|
+
config: LocalSignerConfig,
|
|
113
|
+
deps?: CreateLocalSignerWithProtectionDeps,
|
|
114
|
+
): Promise<{
|
|
115
|
+
signer: ValidatorHASigner;
|
|
116
|
+
db: SlashingProtectionDatabase;
|
|
117
|
+
}> {
|
|
118
|
+
const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
|
|
119
|
+
const dateProvider = deps?.dateProvider ?? new DateProvider();
|
|
120
|
+
|
|
121
|
+
const kvStore = await createStore('signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
|
|
122
|
+
dataDirectory: config.dataDirectory,
|
|
123
|
+
dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
|
|
124
|
+
l1Contracts: config.l1Contracts,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
|
|
128
|
+
|
|
129
|
+
const signerConfig = {
|
|
130
|
+
...config,
|
|
131
|
+
nodeId: config.nodeId || 'local',
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
|
|
135
|
+
|
|
136
|
+
const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
|
|
85
137
|
|
|
86
138
|
return { signer, db };
|
|
87
139
|
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
|
|
143
|
+
* multiple validator nodes in the same process. Used for testing HA setups.
|
|
144
|
+
*/
|
|
145
|
+
export async function createSharedSlashingProtectionDb(
|
|
146
|
+
dateProvider: DateProvider = new DateProvider(),
|
|
147
|
+
): Promise<SlashingProtectionDatabase> {
|
|
148
|
+
const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
|
|
149
|
+
dataStoreMapSizeKb: 1024 * 1024,
|
|
150
|
+
});
|
|
151
|
+
return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
|
|
156
|
+
* Used for testing HA setups where multiple nodes share the same protection database.
|
|
157
|
+
*/
|
|
158
|
+
export function createSignerFromSharedDb(
|
|
159
|
+
db: SlashingProtectionDatabase,
|
|
160
|
+
config: Pick<
|
|
161
|
+
ValidatorHASignerConfig,
|
|
162
|
+
'nodeId' | 'pollingIntervalMs' | 'signingTimeoutMs' | 'maxStuckDutiesAgeMs' | 'l1Contracts'
|
|
163
|
+
>,
|
|
164
|
+
deps?: CreateLocalSignerWithProtectionDeps,
|
|
165
|
+
): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
|
|
166
|
+
const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
|
|
167
|
+
const dateProvider = deps?.dateProvider ?? new DateProvider();
|
|
168
|
+
const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
|
|
169
|
+
const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
|
|
170
|
+
return { signer, db };
|
|
171
|
+
}
|