@aztec/validator-ha-signer 0.0.1-commit.023c3e5
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 +187 -0
- package/dest/config.d.ts +79 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +73 -0
- package/dest/db/index.d.ts +4 -0
- package/dest/db/index.d.ts.map +1 -0
- package/dest/db/index.js +3 -0
- package/dest/db/migrations/1_initial-schema.d.ts +9 -0
- package/dest/db/migrations/1_initial-schema.d.ts.map +1 -0
- package/dest/db/migrations/1_initial-schema.js +20 -0
- package/dest/db/postgres.d.ts +70 -0
- package/dest/db/postgres.d.ts.map +1 -0
- package/dest/db/postgres.js +181 -0
- package/dest/db/schema.d.ts +89 -0
- package/dest/db/schema.d.ts.map +1 -0
- package/dest/db/schema.js +213 -0
- package/dest/db/test_helper.d.ts +10 -0
- package/dest/db/test_helper.d.ts.map +1 -0
- package/dest/db/test_helper.js +14 -0
- package/dest/db/types.d.ts +161 -0
- package/dest/db/types.d.ts.map +1 -0
- package/dest/db/types.js +49 -0
- package/dest/errors.d.ts +34 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +34 -0
- package/dest/factory.d.ts +42 -0
- package/dest/factory.d.ts.map +1 -0
- package/dest/factory.js +70 -0
- package/dest/migrations.d.ts +15 -0
- package/dest/migrations.d.ts.map +1 -0
- package/dest/migrations.js +53 -0
- package/dest/slashing_protection_service.d.ts +80 -0
- package/dest/slashing_protection_service.d.ts.map +1 -0
- package/dest/slashing_protection_service.js +196 -0
- 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 +139 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +21 -0
- package/dest/validator_ha_signer.d.ts +70 -0
- package/dest/validator_ha_signer.d.ts.map +1 -0
- package/dest/validator_ha_signer.js +127 -0
- package/package.json +105 -0
- package/src/config.ts +125 -0
- package/src/db/index.ts +3 -0
- package/src/db/migrations/1_initial-schema.ts +26 -0
- package/src/db/postgres.ts +251 -0
- package/src/db/schema.ts +248 -0
- package/src/db/test_helper.ts +17 -0
- package/src/db/types.ts +201 -0
- package/src/errors.ts +47 -0
- package/src/factory.ts +82 -0
- package/src/migrations.ts +75 -0
- package/src/slashing_protection_service.ts +250 -0
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +207 -0
- package/src/validator_ha_signer.ts +157 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendored pg-compatible Pool/Client wrapper for PGlite.
|
|
3
|
+
*
|
|
4
|
+
* Copied from @middle-management/pglite-pg-adapter v0.0.3
|
|
5
|
+
* https://www.npmjs.com/package/@middle-management/pglite-pg-adapter
|
|
6
|
+
*
|
|
7
|
+
* Modifications:
|
|
8
|
+
* - Converted to ESM and TypeScript
|
|
9
|
+
* - Uses PGliteInterface instead of PGlite class to avoid TypeScript
|
|
10
|
+
* type mismatches from ESM/CJS dual package resolution with private fields
|
|
11
|
+
* - Simplified rowCount calculation to handle CTEs properly
|
|
12
|
+
*/ import { EventEmitter } from 'events';
|
|
13
|
+
import { Readable, Writable } from 'stream';
|
|
14
|
+
export class Client extends EventEmitter {
|
|
15
|
+
pglite;
|
|
16
|
+
_connected = false;
|
|
17
|
+
host;
|
|
18
|
+
port;
|
|
19
|
+
ssl;
|
|
20
|
+
connection;
|
|
21
|
+
// Stub implementations for pg compatibility
|
|
22
|
+
copyFrom = ()=>new Writable();
|
|
23
|
+
copyTo = ()=>new Readable();
|
|
24
|
+
pauseDrain = ()=>{};
|
|
25
|
+
resumeDrain = ()=>{};
|
|
26
|
+
escapeLiteral = (str)=>`'${str.replace(/'/g, "''")}'`;
|
|
27
|
+
escapeIdentifier = (str)=>`"${str.replace(/"/g, '""')}"`;
|
|
28
|
+
setTypeParser = ()=>{};
|
|
29
|
+
getTypeParser = ()=>(value)=>value;
|
|
30
|
+
constructor(config){
|
|
31
|
+
super();
|
|
32
|
+
this.pglite = config.pglite;
|
|
33
|
+
this.host = config.host || 'localhost';
|
|
34
|
+
this.port = config.port || 5432;
|
|
35
|
+
this.ssl = typeof config.ssl === 'boolean' ? config.ssl : !!config.ssl;
|
|
36
|
+
this.connection = {};
|
|
37
|
+
}
|
|
38
|
+
connect() {
|
|
39
|
+
if (this._connected) {
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
this._connected = true;
|
|
43
|
+
this.emit('connect');
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
}
|
|
46
|
+
end() {
|
|
47
|
+
if (!this._connected) {
|
|
48
|
+
return Promise.resolve();
|
|
49
|
+
}
|
|
50
|
+
this._connected = false;
|
|
51
|
+
this.emit('end');
|
|
52
|
+
return Promise.resolve();
|
|
53
|
+
}
|
|
54
|
+
async query(text, values) {
|
|
55
|
+
if (!this._connected) {
|
|
56
|
+
throw new Error('Client is not connected');
|
|
57
|
+
}
|
|
58
|
+
const result = await this.pglite.query(text, values);
|
|
59
|
+
return this.convertPGliteResult(result);
|
|
60
|
+
}
|
|
61
|
+
convertPGliteResult(result) {
|
|
62
|
+
return {
|
|
63
|
+
command: '',
|
|
64
|
+
rowCount: 'affectedRows' in result ? result.affectedRows ?? 0 : result.rows.length,
|
|
65
|
+
oid: 0,
|
|
66
|
+
fields: result.fields.map((field)=>({
|
|
67
|
+
name: field.name,
|
|
68
|
+
tableID: 0,
|
|
69
|
+
columnID: 0,
|
|
70
|
+
dataTypeID: field.dataTypeID,
|
|
71
|
+
dataTypeSize: -1,
|
|
72
|
+
dataTypeModifier: -1,
|
|
73
|
+
format: 'text'
|
|
74
|
+
})),
|
|
75
|
+
rows: result.rows
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
get connected() {
|
|
79
|
+
return this._connected;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export class Pool extends EventEmitter {
|
|
83
|
+
clients = [];
|
|
84
|
+
availableClients = [];
|
|
85
|
+
waitingQueue = [];
|
|
86
|
+
_ended = false;
|
|
87
|
+
pglite;
|
|
88
|
+
_config;
|
|
89
|
+
expiredCount = 0;
|
|
90
|
+
options;
|
|
91
|
+
constructor(config){
|
|
92
|
+
super();
|
|
93
|
+
this._config = {
|
|
94
|
+
max: 10,
|
|
95
|
+
min: 0,
|
|
96
|
+
...config
|
|
97
|
+
};
|
|
98
|
+
this.pglite = config.pglite;
|
|
99
|
+
this.options = config;
|
|
100
|
+
}
|
|
101
|
+
get totalCount() {
|
|
102
|
+
return this.clients.length;
|
|
103
|
+
}
|
|
104
|
+
get idleCount() {
|
|
105
|
+
return this.availableClients.length;
|
|
106
|
+
}
|
|
107
|
+
get waitingCount() {
|
|
108
|
+
return this.waitingQueue.length;
|
|
109
|
+
}
|
|
110
|
+
get ending() {
|
|
111
|
+
return this._ended;
|
|
112
|
+
}
|
|
113
|
+
get ended() {
|
|
114
|
+
return this._ended;
|
|
115
|
+
}
|
|
116
|
+
connect() {
|
|
117
|
+
if (this._ended) {
|
|
118
|
+
return Promise.reject(new Error('Pool is ended'));
|
|
119
|
+
}
|
|
120
|
+
if (this.availableClients.length > 0) {
|
|
121
|
+
const client = this.availableClients.pop();
|
|
122
|
+
client._markInUse();
|
|
123
|
+
return Promise.resolve(client);
|
|
124
|
+
}
|
|
125
|
+
if (this.clients.length < (this._config.max || 10)) {
|
|
126
|
+
const client = new PoolClient(this.pglite, this);
|
|
127
|
+
this.clients.push(client);
|
|
128
|
+
return Promise.resolve(client);
|
|
129
|
+
}
|
|
130
|
+
return new Promise((resolve)=>{
|
|
131
|
+
this.waitingQueue.push(resolve);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async query(text, values) {
|
|
135
|
+
const client = await this.connect();
|
|
136
|
+
try {
|
|
137
|
+
return await client.query(text, values);
|
|
138
|
+
} finally{
|
|
139
|
+
client.release();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
releaseClient(client) {
|
|
143
|
+
const index = this.clients.indexOf(client);
|
|
144
|
+
if (index !== -1) {
|
|
145
|
+
client._markAvailable();
|
|
146
|
+
if (this.waitingQueue.length > 0) {
|
|
147
|
+
const resolve = this.waitingQueue.shift();
|
|
148
|
+
client._markInUse();
|
|
149
|
+
resolve(client);
|
|
150
|
+
} else {
|
|
151
|
+
this.availableClients.push(client);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
end() {
|
|
156
|
+
this._ended = true;
|
|
157
|
+
this.clients.forEach((client)=>client._markReleased());
|
|
158
|
+
this.clients = [];
|
|
159
|
+
this.availableClients = [];
|
|
160
|
+
this.emit('end');
|
|
161
|
+
return Promise.resolve();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export class PoolClient extends Client {
|
|
165
|
+
pool;
|
|
166
|
+
_released = false;
|
|
167
|
+
_inUse = true;
|
|
168
|
+
_userReleased = false;
|
|
169
|
+
constructor(pglite, pool){
|
|
170
|
+
super({
|
|
171
|
+
pglite
|
|
172
|
+
});
|
|
173
|
+
this.pool = pool;
|
|
174
|
+
this._connected = true;
|
|
175
|
+
}
|
|
176
|
+
async query(text, values) {
|
|
177
|
+
if (this._userReleased && !this._inUse) {
|
|
178
|
+
throw new Error('Client has been released back to the pool');
|
|
179
|
+
}
|
|
180
|
+
const result = await this.pglite.query(text, values);
|
|
181
|
+
return this.convertPGliteResult(result);
|
|
182
|
+
}
|
|
183
|
+
release() {
|
|
184
|
+
if (this._released || this._userReleased) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this._userReleased = true;
|
|
188
|
+
this.pool.releaseClient(this);
|
|
189
|
+
}
|
|
190
|
+
end() {
|
|
191
|
+
this.release();
|
|
192
|
+
return Promise.resolve();
|
|
193
|
+
}
|
|
194
|
+
_markInUse() {
|
|
195
|
+
this._inUse = true;
|
|
196
|
+
this._userReleased = false;
|
|
197
|
+
}
|
|
198
|
+
_markAvailable() {
|
|
199
|
+
this._inUse = false;
|
|
200
|
+
this._userReleased = false;
|
|
201
|
+
}
|
|
202
|
+
_markReleased() {
|
|
203
|
+
this._released = true;
|
|
204
|
+
this._inUse = false;
|
|
205
|
+
this._userReleased = true;
|
|
206
|
+
}
|
|
207
|
+
get connected() {
|
|
208
|
+
return this._connected && !this._released;
|
|
209
|
+
}
|
|
210
|
+
}
|
package/dest/types.d.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { BlockNumber, type CheckpointNumber, type IndexWithinCheckpoint, type SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
|
+
import type { Pool } from 'pg';
|
|
4
|
+
import type { ValidatorHASignerConfig } from './config.js';
|
|
5
|
+
import { type BlockProposalDutyIdentifier, type CheckAndRecordParams, type DeleteDutyParams, type DutyIdentifier, DutyType, type OtherDutyIdentifier, type RecordSuccessParams, type ValidatorDutyRecord } from './db/types.js';
|
|
6
|
+
export type { BlockProposalDutyIdentifier, CheckAndRecordParams, DeleteDutyParams, DutyIdentifier, OtherDutyIdentifier, RecordSuccessParams, ValidatorDutyRecord, ValidatorHASignerConfig, };
|
|
7
|
+
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Result of tryInsertOrGetExisting operation
|
|
10
|
+
*/
|
|
11
|
+
export interface TryInsertOrGetResult {
|
|
12
|
+
/** True if we inserted a new record, false if we got an existing record */
|
|
13
|
+
isNew: boolean;
|
|
14
|
+
/** The record (either newly inserted or existing) */
|
|
15
|
+
record: ValidatorDutyRecord;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* deps for creating an HA signer
|
|
19
|
+
*/
|
|
20
|
+
export interface CreateHASignerDeps {
|
|
21
|
+
/**
|
|
22
|
+
* Optional PostgreSQL connection pool
|
|
23
|
+
* If provided, databaseUrl and poolConfig are ignored
|
|
24
|
+
*/
|
|
25
|
+
pool?: Pool;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Base context for signing operations
|
|
29
|
+
*/
|
|
30
|
+
interface BaseSigningContext {
|
|
31
|
+
/** Slot number for this duty */
|
|
32
|
+
slot: SlotNumber;
|
|
33
|
+
/**
|
|
34
|
+
* Block or checkpoint number for this duty.
|
|
35
|
+
* For block proposals, this is the block number.
|
|
36
|
+
* For checkpoint proposals, this is the checkpoint number.
|
|
37
|
+
*/
|
|
38
|
+
blockNumber: BlockNumber | CheckpointNumber;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Signing context for block proposals.
|
|
42
|
+
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
43
|
+
*/
|
|
44
|
+
export interface BlockProposalSigningContext extends BaseSigningContext {
|
|
45
|
+
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
46
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint;
|
|
47
|
+
dutyType: DutyType.BLOCK_PROPOSAL;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Signing context for non-block-proposal duties that require HA protection.
|
|
51
|
+
* blockIndexWithinCheckpoint is not applicable (internally always -1).
|
|
52
|
+
*/
|
|
53
|
+
export interface OtherSigningContext extends BaseSigningContext {
|
|
54
|
+
dutyType: DutyType.CHECKPOINT_PROPOSAL | DutyType.ATTESTATION | DutyType.ATTESTATIONS_AND_SIGNERS;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Signing context for governance/slashing votes which only need slot for HA protection.
|
|
58
|
+
* blockNumber is not applicable (internally always 0).
|
|
59
|
+
*/
|
|
60
|
+
export interface VoteSigningContext {
|
|
61
|
+
slot: SlotNumber;
|
|
62
|
+
dutyType: DutyType.GOVERNANCE_VOTE | DutyType.SLASHING_VOTE;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Signing context for duties which don't require slot/blockNumber
|
|
66
|
+
* as they don't need HA protection (AUTH_REQUEST, TXS).
|
|
67
|
+
*/
|
|
68
|
+
export interface NoHAProtectionSigningContext {
|
|
69
|
+
dutyType: DutyType.AUTH_REQUEST | DutyType.TXS;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Signing contexts that require HA protection (excludes AUTH_REQUEST).
|
|
73
|
+
* Used by the HA signer's signWithProtection method.
|
|
74
|
+
*/
|
|
75
|
+
export type HAProtectedSigningContext = BlockProposalSigningContext | OtherSigningContext | VoteSigningContext;
|
|
76
|
+
/**
|
|
77
|
+
* Type guard to check if a SigningContext requires HA protection.
|
|
78
|
+
* Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
|
|
79
|
+
*/
|
|
80
|
+
export declare function isHAProtectedContext(context: SigningContext): context is HAProtectedSigningContext;
|
|
81
|
+
/**
|
|
82
|
+
* Gets the block number from a signing context.
|
|
83
|
+
* - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
|
|
84
|
+
* - Other duties: returns the blockNumber from the context
|
|
85
|
+
*/
|
|
86
|
+
export declare function getBlockNumberFromSigningContext(context: HAProtectedSigningContext): BlockNumber | CheckpointNumber;
|
|
87
|
+
/**
|
|
88
|
+
* Context required for slashing protection during signing operations.
|
|
89
|
+
* Uses discriminated union to enforce type safety:
|
|
90
|
+
* - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
|
|
91
|
+
* - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
|
|
92
|
+
* - Vote duties only need slot (blockNumber is internally 0)
|
|
93
|
+
* - AUTH_REQUEST and TXS duties don't need slot/blockNumber (no HA protection needed)
|
|
94
|
+
*/
|
|
95
|
+
export type SigningContext = HAProtectedSigningContext | NoHAProtectionSigningContext;
|
|
96
|
+
/**
|
|
97
|
+
* Database interface for slashing protection operations
|
|
98
|
+
* This abstraction allows for different database implementations (PostgreSQL, SQLite, etc.)
|
|
99
|
+
*
|
|
100
|
+
* The interface is designed around 3 core operations:
|
|
101
|
+
* 1. tryInsertOrGetExisting - Atomically insert or get existing record (eliminates race conditions)
|
|
102
|
+
* 2. updateDutySigned - Update to signed status on success
|
|
103
|
+
* 3. deleteDuty - Delete a duty record on failure
|
|
104
|
+
*/
|
|
105
|
+
export interface SlashingProtectionDatabase {
|
|
106
|
+
/**
|
|
107
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
108
|
+
*
|
|
109
|
+
* @returns { isNew: true, record } if we successfully inserted and acquired the lock
|
|
110
|
+
* @returns { isNew: false, record } if a record already exists (caller should handle based on status)
|
|
111
|
+
*/
|
|
112
|
+
tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult>;
|
|
113
|
+
/**
|
|
114
|
+
* Update a duty to 'signed' status with the signature.
|
|
115
|
+
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
116
|
+
*
|
|
117
|
+
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
118
|
+
*/
|
|
119
|
+
updateDutySigned(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, signature: string, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
120
|
+
/**
|
|
121
|
+
* Delete a duty record.
|
|
122
|
+
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
123
|
+
* Used when signing fails to allow another node/attempt to retry.
|
|
124
|
+
*
|
|
125
|
+
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
126
|
+
*/
|
|
127
|
+
deleteDuty(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
128
|
+
/**
|
|
129
|
+
* Cleanup own stuck duties
|
|
130
|
+
* @returns the number of duties cleaned up
|
|
131
|
+
*/
|
|
132
|
+
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
133
|
+
/**
|
|
134
|
+
* Close the database connection.
|
|
135
|
+
* Should be called during graceful shutdown.
|
|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B,CAAC,CAAC;IAEpF;;;;;OAKG;IACH,gBAAgB,CACd,gBAAgB,EAAE,UAAU,EAC5B,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,0BAA0B,EAAE,MAAM,GACjC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEpB;;;;;;OAMG;IACH,UAAU,CACR,gBAAgB,EAAE,UAAU,EAC5B,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,0BAA0B,EAAE,MAAM,GACjC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEpB;;;OAGG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEzE;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
|
package/dest/types.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import { DutyType } from './db/types.js';
|
|
3
|
+
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Type guard to check if a SigningContext requires HA protection.
|
|
6
|
+
* Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
|
|
7
|
+
*/ export function isHAProtectedContext(context) {
|
|
8
|
+
return context.dutyType !== DutyType.AUTH_REQUEST && context.dutyType !== DutyType.TXS;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Gets the block number from a signing context.
|
|
12
|
+
* - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
|
|
13
|
+
* - Other duties: returns the blockNumber from the context
|
|
14
|
+
*/ export function getBlockNumberFromSigningContext(context) {
|
|
15
|
+
// Check for duty types that have blockNumber
|
|
16
|
+
if (context.dutyType === DutyType.BLOCK_PROPOSAL || context.dutyType === DutyType.CHECKPOINT_PROPOSAL || context.dutyType === DutyType.ATTESTATION || context.dutyType === DutyType.ATTESTATIONS_AND_SIGNERS) {
|
|
17
|
+
return context.blockNumber;
|
|
18
|
+
}
|
|
19
|
+
// Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE) don't have blockNumber
|
|
20
|
+
return BlockNumber(0);
|
|
21
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validator High Availability Signer
|
|
3
|
+
*
|
|
4
|
+
* Wraps signing operations with distributed locking and slashing protection.
|
|
5
|
+
* This ensures that even with multiple validator nodes running, only one
|
|
6
|
+
* node will sign for a given duty (slot + duty type).
|
|
7
|
+
*/
|
|
8
|
+
import type { Buffer32 } from '@aztec/foundation/buffer';
|
|
9
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
10
|
+
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
11
|
+
import type { ValidatorHASignerConfig } from './config.js';
|
|
12
|
+
import { type HAProtectedSigningContext, type SlashingProtectionDatabase } from './types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Validator High Availability Signer
|
|
15
|
+
*
|
|
16
|
+
* Provides signing capabilities with distributed locking for validators
|
|
17
|
+
* in a high-availability setup.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* ```
|
|
21
|
+
* const signer = new ValidatorHASigner(db, config);
|
|
22
|
+
*
|
|
23
|
+
* // Sign with slashing protection
|
|
24
|
+
* const signature = await signer.signWithProtection(
|
|
25
|
+
* validatorAddress,
|
|
26
|
+
* messageHash,
|
|
27
|
+
* { slot: 100n, blockNumber: 50n, dutyType: 'BLOCK_PROPOSAL' },
|
|
28
|
+
* async (root) => localSigner.signMessage(root),
|
|
29
|
+
* );
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare class ValidatorHASigner {
|
|
33
|
+
private readonly config;
|
|
34
|
+
private readonly log;
|
|
35
|
+
private readonly slashingProtection;
|
|
36
|
+
constructor(db: SlashingProtectionDatabase, config: ValidatorHASignerConfig);
|
|
37
|
+
/**
|
|
38
|
+
* Sign a message with slashing protection.
|
|
39
|
+
*
|
|
40
|
+
* This method:
|
|
41
|
+
* 1. Acquires a distributed lock for (validator, slot, dutyType)
|
|
42
|
+
* 2. Calls the provided signing function
|
|
43
|
+
* 3. Records the result (success or failure)
|
|
44
|
+
*
|
|
45
|
+
* @param validatorAddress - The validator's Ethereum address
|
|
46
|
+
* @param messageHash - The hash to be signed
|
|
47
|
+
* @param context - The signing context (HA-protected duty types only)
|
|
48
|
+
* @param signFn - Function that performs the actual signing
|
|
49
|
+
* @returns The signature
|
|
50
|
+
*
|
|
51
|
+
* @throws DutyAlreadySignedError if the duty was already signed (expected in HA)
|
|
52
|
+
* @throws SlashingProtectionError if attempting to sign different data for same slot (expected in HA)
|
|
53
|
+
*/
|
|
54
|
+
signWithProtection(validatorAddress: EthAddress, messageHash: Buffer32, context: HAProtectedSigningContext, signFn: (messageHash: Buffer32) => Promise<Signature>): Promise<Signature>;
|
|
55
|
+
/**
|
|
56
|
+
* Get the node ID for this signer
|
|
57
|
+
*/
|
|
58
|
+
get nodeId(): string;
|
|
59
|
+
/**
|
|
60
|
+
* Start the HA signer background tasks (cleanup of stuck duties).
|
|
61
|
+
* Should be called after construction and before signing operations.
|
|
62
|
+
*/
|
|
63
|
+
start(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Stop the HA signer background tasks and close database connection.
|
|
66
|
+
* Should be called during graceful shutdown.
|
|
67
|
+
*/
|
|
68
|
+
stop(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdG9yX2hhX3NpZ25lci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3ZhbGlkY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7T0FHRztJQUNILEtBQUssU0FFSjtJQUVEOzs7T0FHRztJQUNHLElBQUksa0JBR1Q7Q0FDRiJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator_ha_signer.d.ts","sourceRoot":"","sources":["../src/validator_ha_signer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAGjE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAG3D,OAAO,EACL,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAEhC,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,iBAAiB;IAM1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IALzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA4B;IAE/D,YACE,EAAE,EAAE,0BAA0B,EACb,MAAM,EAAE,uBAAuB,EAgBjD;IAED;;;;;;;;;;;;;;;;OAgBG;IACG,kBAAkB,CACtB,gBAAgB,EAAE,UAAU,EAC5B,WAAW,EAAE,QAAQ,EACrB,OAAO,EAAE,yBAAyB,EAClC,MAAM,EAAE,CAAC,WAAW,EAAE,QAAQ,KAAK,OAAO,CAAC,SAAS,CAAC,GACpD,OAAO,CAAC,SAAS,CAAC,CA6CpB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;OAGG;IACH,KAAK,SAEJ;IAED;;;OAGG;IACG,IAAI,kBAGT;CACF"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validator High Availability Signer
|
|
3
|
+
*
|
|
4
|
+
* Wraps signing operations with distributed locking and slashing protection.
|
|
5
|
+
* This ensures that even with multiple validator nodes running, only one
|
|
6
|
+
* node will sign for a given duty (slot + duty type).
|
|
7
|
+
*/ import { createLogger } from '@aztec/foundation/log';
|
|
8
|
+
import { DutyType } from './db/types.js';
|
|
9
|
+
import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
10
|
+
import { getBlockNumberFromSigningContext } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Validator High Availability Signer
|
|
13
|
+
*
|
|
14
|
+
* Provides signing capabilities with distributed locking for validators
|
|
15
|
+
* in a high-availability setup.
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
* ```
|
|
19
|
+
* const signer = new ValidatorHASigner(db, config);
|
|
20
|
+
*
|
|
21
|
+
* // Sign with slashing protection
|
|
22
|
+
* const signature = await signer.signWithProtection(
|
|
23
|
+
* validatorAddress,
|
|
24
|
+
* messageHash,
|
|
25
|
+
* { slot: 100n, blockNumber: 50n, dutyType: 'BLOCK_PROPOSAL' },
|
|
26
|
+
* async (root) => localSigner.signMessage(root),
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*/ export class ValidatorHASigner {
|
|
30
|
+
config;
|
|
31
|
+
log;
|
|
32
|
+
slashingProtection;
|
|
33
|
+
constructor(db, config){
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.log = createLogger('validator-ha-signer');
|
|
36
|
+
if (!config.haSigningEnabled) {
|
|
37
|
+
// this shouldn't happen, the validator should use different signer for non-HA setups
|
|
38
|
+
throw new Error('Validator HA Signer is not enabled in config');
|
|
39
|
+
}
|
|
40
|
+
if (!config.nodeId || config.nodeId === '') {
|
|
41
|
+
throw new Error('NODE_ID is required for high-availability setups');
|
|
42
|
+
}
|
|
43
|
+
this.slashingProtection = new SlashingProtectionService(db, config);
|
|
44
|
+
this.log.info('Validator HA Signer initialized with slashing protection', {
|
|
45
|
+
nodeId: config.nodeId
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sign a message with slashing protection.
|
|
50
|
+
*
|
|
51
|
+
* This method:
|
|
52
|
+
* 1. Acquires a distributed lock for (validator, slot, dutyType)
|
|
53
|
+
* 2. Calls the provided signing function
|
|
54
|
+
* 3. Records the result (success or failure)
|
|
55
|
+
*
|
|
56
|
+
* @param validatorAddress - The validator's Ethereum address
|
|
57
|
+
* @param messageHash - The hash to be signed
|
|
58
|
+
* @param context - The signing context (HA-protected duty types only)
|
|
59
|
+
* @param signFn - Function that performs the actual signing
|
|
60
|
+
* @returns The signature
|
|
61
|
+
*
|
|
62
|
+
* @throws DutyAlreadySignedError if the duty was already signed (expected in HA)
|
|
63
|
+
* @throws SlashingProtectionError if attempting to sign different data for same slot (expected in HA)
|
|
64
|
+
*/ async signWithProtection(validatorAddress, messageHash, context, signFn) {
|
|
65
|
+
let dutyIdentifier;
|
|
66
|
+
if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
67
|
+
dutyIdentifier = {
|
|
68
|
+
validatorAddress,
|
|
69
|
+
slot: context.slot,
|
|
70
|
+
blockIndexWithinCheckpoint: context.blockIndexWithinCheckpoint,
|
|
71
|
+
dutyType: context.dutyType
|
|
72
|
+
};
|
|
73
|
+
} else {
|
|
74
|
+
dutyIdentifier = {
|
|
75
|
+
validatorAddress,
|
|
76
|
+
slot: context.slot,
|
|
77
|
+
dutyType: context.dutyType
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// Acquire lock and get the token for ownership verification
|
|
81
|
+
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
82
|
+
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
83
|
+
...dutyIdentifier,
|
|
84
|
+
blockNumber,
|
|
85
|
+
messageHash: messageHash.toString(),
|
|
86
|
+
nodeId: this.config.nodeId
|
|
87
|
+
});
|
|
88
|
+
// Perform signing
|
|
89
|
+
let signature;
|
|
90
|
+
try {
|
|
91
|
+
signature = await signFn(messageHash);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
// Delete duty to allow retry (only succeeds if we own the lock)
|
|
94
|
+
await this.slashingProtection.deleteDuty({
|
|
95
|
+
...dutyIdentifier,
|
|
96
|
+
lockToken
|
|
97
|
+
});
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
// Record success (only succeeds if we own the lock)
|
|
101
|
+
await this.slashingProtection.recordSuccess({
|
|
102
|
+
...dutyIdentifier,
|
|
103
|
+
signature,
|
|
104
|
+
nodeId: this.config.nodeId,
|
|
105
|
+
lockToken
|
|
106
|
+
});
|
|
107
|
+
return signature;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get the node ID for this signer
|
|
111
|
+
*/ get nodeId() {
|
|
112
|
+
return this.config.nodeId;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Start the HA signer background tasks (cleanup of stuck duties).
|
|
116
|
+
* Should be called after construction and before signing operations.
|
|
117
|
+
*/ start() {
|
|
118
|
+
this.slashingProtection.start();
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Stop the HA signer background tasks and close database connection.
|
|
122
|
+
* Should be called during graceful shutdown.
|
|
123
|
+
*/ async stop() {
|
|
124
|
+
await this.slashingProtection.stop();
|
|
125
|
+
await this.slashingProtection.close();
|
|
126
|
+
}
|
|
127
|
+
}
|