@aztec/validator-ha-signer 4.0.0-nightly.20260110
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 +182 -0
- package/dest/config.d.ts +47 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +64 -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 +55 -0
- package/dest/db/postgres.d.ts.map +1 -0
- package/dest/db/postgres.js +150 -0
- package/dest/db/schema.d.ts +85 -0
- package/dest/db/schema.d.ts.map +1 -0
- package/dest/db/schema.js +201 -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 +109 -0
- package/dest/db/types.d.ts.map +1 -0
- package/dest/db/types.js +15 -0
- package/dest/errors.d.ts +30 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +31 -0
- package/dest/factory.d.ts +50 -0
- package/dest/factory.d.ts.map +1 -0
- package/dest/factory.js +75 -0
- package/dest/migrations.d.ts +15 -0
- package/dest/migrations.d.ts.map +1 -0
- package/dest/migrations.js +42 -0
- package/dest/slashing_protection_service.d.ts +74 -0
- package/dest/slashing_protection_service.d.ts.map +1 -0
- package/dest/slashing_protection_service.js +184 -0
- package/dest/types.d.ts +75 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +1 -0
- package/dest/validator_ha_signer.d.ts +74 -0
- package/dest/validator_ha_signer.d.ts.map +1 -0
- package/dest/validator_ha_signer.js +131 -0
- package/package.json +105 -0
- package/src/config.ts +116 -0
- package/src/db/index.ts +3 -0
- package/src/db/migrations/1_initial-schema.ts +26 -0
- package/src/db/postgres.ts +202 -0
- package/src/db/schema.ts +236 -0
- package/src/db/test_helper.ts +17 -0
- package/src/db/types.ts +117 -0
- package/src/errors.ts +42 -0
- package/src/factory.ts +87 -0
- package/src/migrations.ts +59 -0
- package/src/slashing_protection_service.ts +216 -0
- package/src/types.ts +105 -0
- package/src/validator_ha_signer.ts +164 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL schema for the validator_duties table
|
|
3
|
+
*
|
|
4
|
+
* This table is used for distributed locking and slashing protection across multiple validator nodes.
|
|
5
|
+
* The PRIMARY KEY constraint ensures that only one node can acquire the lock for a given validator,
|
|
6
|
+
* slot, and duty type combination.
|
|
7
|
+
*/ /**
|
|
8
|
+
* Current schema version
|
|
9
|
+
*/ export const SCHEMA_VERSION = 1;
|
|
10
|
+
/**
|
|
11
|
+
* SQL to create the validator_duties table
|
|
12
|
+
*/ export const CREATE_VALIDATOR_DUTIES_TABLE = `
|
|
13
|
+
CREATE TABLE IF NOT EXISTS validator_duties (
|
|
14
|
+
validator_address VARCHAR(42) NOT NULL,
|
|
15
|
+
slot BIGINT NOT NULL,
|
|
16
|
+
block_number BIGINT NOT NULL,
|
|
17
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS')),
|
|
18
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed', 'failed')),
|
|
19
|
+
message_hash VARCHAR(66) NOT NULL,
|
|
20
|
+
signature VARCHAR(132),
|
|
21
|
+
node_id VARCHAR(255) NOT NULL,
|
|
22
|
+
lock_token VARCHAR(64) NOT NULL,
|
|
23
|
+
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
24
|
+
completed_at TIMESTAMP,
|
|
25
|
+
error_message TEXT,
|
|
26
|
+
|
|
27
|
+
PRIMARY KEY (validator_address, slot, duty_type),
|
|
28
|
+
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
29
|
+
);
|
|
30
|
+
`;
|
|
31
|
+
/**
|
|
32
|
+
* SQL to create index on status and started_at for cleanup queries
|
|
33
|
+
*/ export const CREATE_STATUS_INDEX = `
|
|
34
|
+
CREATE INDEX IF NOT EXISTS idx_validator_duties_status
|
|
35
|
+
ON validator_duties(status, started_at);
|
|
36
|
+
`;
|
|
37
|
+
/**
|
|
38
|
+
* SQL to create index for querying duties by node
|
|
39
|
+
*/ export const CREATE_NODE_INDEX = `
|
|
40
|
+
CREATE INDEX IF NOT EXISTS idx_validator_duties_node
|
|
41
|
+
ON validator_duties(node_id, started_at);
|
|
42
|
+
`;
|
|
43
|
+
/**
|
|
44
|
+
* SQL to create the schema_version table for tracking migrations
|
|
45
|
+
*/ export const CREATE_SCHEMA_VERSION_TABLE = `
|
|
46
|
+
CREATE TABLE IF NOT EXISTS schema_version (
|
|
47
|
+
version INTEGER PRIMARY KEY,
|
|
48
|
+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
49
|
+
);
|
|
50
|
+
`;
|
|
51
|
+
/**
|
|
52
|
+
* SQL to initialize schema version
|
|
53
|
+
*/ export const INSERT_SCHEMA_VERSION = `
|
|
54
|
+
INSERT INTO schema_version (version)
|
|
55
|
+
VALUES ($1)
|
|
56
|
+
ON CONFLICT (version) DO NOTHING;
|
|
57
|
+
`;
|
|
58
|
+
/**
|
|
59
|
+
* Complete schema setup - all statements in order
|
|
60
|
+
*/ export const SCHEMA_SETUP = [
|
|
61
|
+
CREATE_SCHEMA_VERSION_TABLE,
|
|
62
|
+
CREATE_VALIDATOR_DUTIES_TABLE,
|
|
63
|
+
CREATE_STATUS_INDEX,
|
|
64
|
+
CREATE_NODE_INDEX
|
|
65
|
+
];
|
|
66
|
+
/**
|
|
67
|
+
* Query to get current schema version
|
|
68
|
+
*/ export const GET_SCHEMA_VERSION = `
|
|
69
|
+
SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
70
|
+
`;
|
|
71
|
+
/**
|
|
72
|
+
* Atomic insert-or-get query.
|
|
73
|
+
* Tries to insert a new duty record. If a record already exists (conflict),
|
|
74
|
+
* returns the existing record instead.
|
|
75
|
+
*
|
|
76
|
+
* Returns the record with an `is_new` flag indicating whether we inserted or got existing.
|
|
77
|
+
*/ export const INSERT_OR_GET_DUTY = `
|
|
78
|
+
WITH inserted AS (
|
|
79
|
+
INSERT INTO validator_duties (
|
|
80
|
+
validator_address,
|
|
81
|
+
slot,
|
|
82
|
+
block_number,
|
|
83
|
+
duty_type,
|
|
84
|
+
status,
|
|
85
|
+
message_hash,
|
|
86
|
+
node_id,
|
|
87
|
+
lock_token,
|
|
88
|
+
started_at
|
|
89
|
+
) VALUES ($1, $2, $3, $4, 'signing', $5, $6, $7, CURRENT_TIMESTAMP)
|
|
90
|
+
ON CONFLICT (validator_address, slot, duty_type) DO NOTHING
|
|
91
|
+
RETURNING
|
|
92
|
+
validator_address,
|
|
93
|
+
slot,
|
|
94
|
+
block_number,
|
|
95
|
+
duty_type,
|
|
96
|
+
status,
|
|
97
|
+
message_hash,
|
|
98
|
+
signature,
|
|
99
|
+
node_id,
|
|
100
|
+
lock_token,
|
|
101
|
+
started_at,
|
|
102
|
+
completed_at,
|
|
103
|
+
error_message,
|
|
104
|
+
TRUE as is_new
|
|
105
|
+
)
|
|
106
|
+
SELECT * FROM inserted
|
|
107
|
+
UNION ALL
|
|
108
|
+
SELECT
|
|
109
|
+
validator_address,
|
|
110
|
+
slot,
|
|
111
|
+
block_number,
|
|
112
|
+
duty_type,
|
|
113
|
+
status,
|
|
114
|
+
message_hash,
|
|
115
|
+
signature,
|
|
116
|
+
node_id,
|
|
117
|
+
'' as lock_token,
|
|
118
|
+
started_at,
|
|
119
|
+
completed_at,
|
|
120
|
+
error_message,
|
|
121
|
+
FALSE as is_new
|
|
122
|
+
FROM validator_duties
|
|
123
|
+
WHERE validator_address = $1
|
|
124
|
+
AND slot = $2
|
|
125
|
+
AND duty_type = $4
|
|
126
|
+
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
127
|
+
`;
|
|
128
|
+
/**
|
|
129
|
+
* Query to update a duty to 'signed' status
|
|
130
|
+
*/ export const UPDATE_DUTY_SIGNED = `
|
|
131
|
+
UPDATE validator_duties
|
|
132
|
+
SET status = 'signed',
|
|
133
|
+
signature = $1,
|
|
134
|
+
completed_at = CURRENT_TIMESTAMP
|
|
135
|
+
WHERE validator_address = $2
|
|
136
|
+
AND slot = $3
|
|
137
|
+
AND duty_type = $4
|
|
138
|
+
AND status = 'signing'
|
|
139
|
+
AND lock_token = $5;
|
|
140
|
+
`;
|
|
141
|
+
/**
|
|
142
|
+
* Query to delete a duty
|
|
143
|
+
* Only deletes if the lockToken matches
|
|
144
|
+
*/ export const DELETE_DUTY = `
|
|
145
|
+
DELETE FROM validator_duties
|
|
146
|
+
WHERE validator_address = $1
|
|
147
|
+
AND slot = $2
|
|
148
|
+
AND duty_type = $3
|
|
149
|
+
AND status = 'signing'
|
|
150
|
+
AND lock_token = $4;
|
|
151
|
+
`;
|
|
152
|
+
/**
|
|
153
|
+
* Query to clean up old signed duties (for maintenance)
|
|
154
|
+
* Removes signed duties older than a specified timestamp
|
|
155
|
+
*/ export const CLEANUP_OLD_SIGNED_DUTIES = `
|
|
156
|
+
DELETE FROM validator_duties
|
|
157
|
+
WHERE status = 'signed'
|
|
158
|
+
AND completed_at < $1;
|
|
159
|
+
`;
|
|
160
|
+
/**
|
|
161
|
+
* Query to clean up old duties (for maintenance)
|
|
162
|
+
* Removes duties older than a specified timestamp
|
|
163
|
+
*/ export const CLEANUP_OLD_DUTIES = `
|
|
164
|
+
DELETE FROM validator_duties
|
|
165
|
+
WHERE status IN ('signing', 'signed', 'failed')
|
|
166
|
+
AND started_at < $1;
|
|
167
|
+
`;
|
|
168
|
+
/**
|
|
169
|
+
* Query to cleanup own stuck duties
|
|
170
|
+
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
171
|
+
*/ export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
172
|
+
DELETE FROM validator_duties
|
|
173
|
+
WHERE node_id = $1
|
|
174
|
+
AND status = 'signing'
|
|
175
|
+
AND started_at < $2;
|
|
176
|
+
`;
|
|
177
|
+
/**
|
|
178
|
+
* SQL to drop the validator_duties table
|
|
179
|
+
*/ export const DROP_VALIDATOR_DUTIES_TABLE = `DROP TABLE IF EXISTS validator_duties;`;
|
|
180
|
+
/**
|
|
181
|
+
* SQL to drop the schema_version table
|
|
182
|
+
*/ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
|
|
183
|
+
/**
|
|
184
|
+
* Query to get stuck duties (for monitoring/alerting)
|
|
185
|
+
* Returns duties in 'signing' status that have been stuck for too long
|
|
186
|
+
*/ export const GET_STUCK_DUTIES = `
|
|
187
|
+
SELECT
|
|
188
|
+
validator_address,
|
|
189
|
+
slot,
|
|
190
|
+
block_number,
|
|
191
|
+
duty_type,
|
|
192
|
+
status,
|
|
193
|
+
message_hash,
|
|
194
|
+
node_id,
|
|
195
|
+
started_at,
|
|
196
|
+
EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds
|
|
197
|
+
FROM validator_duties
|
|
198
|
+
WHERE status = 'signing'
|
|
199
|
+
AND started_at < $1
|
|
200
|
+
ORDER BY started_at ASC;
|
|
201
|
+
`;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers for database setup
|
|
3
|
+
*/
|
|
4
|
+
import type { PGlite } from '@electric-sql/pglite';
|
|
5
|
+
/**
|
|
6
|
+
* Set up the database schema for testing.
|
|
7
|
+
* This runs the same SQL that migrations would apply.
|
|
8
|
+
*/
|
|
9
|
+
export declare function setupTestSchema(pglite: PGlite): Promise<void>;
|
|
10
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdF9oZWxwZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi90ZXN0X2hlbHBlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUNILE9BQU8sS0FBSyxFQUFFLE1BQU0sRUFBRSxNQUFNLHNCQUFzQixDQUFDO0FBSW5EOzs7R0FHRztBQUNILHdCQUFzQixlQUFlLENBQUMsTUFBTSxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBS25FIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test_helper.d.ts","sourceRoot":"","sources":["../../src/db/test_helper.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAInD;;;GAGG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAKnE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers for database setup
|
|
3
|
+
*/ import { INSERT_SCHEMA_VERSION, SCHEMA_SETUP, SCHEMA_VERSION } from './schema.js';
|
|
4
|
+
/**
|
|
5
|
+
* Set up the database schema for testing.
|
|
6
|
+
* This runs the same SQL that migrations would apply.
|
|
7
|
+
*/ export async function setupTestSchema(pglite) {
|
|
8
|
+
for (const statement of SCHEMA_SETUP){
|
|
9
|
+
await pglite.query(statement);
|
|
10
|
+
}
|
|
11
|
+
await pglite.query(INSERT_SCHEMA_VERSION, [
|
|
12
|
+
SCHEMA_VERSION
|
|
13
|
+
]);
|
|
14
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
+
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
3
|
+
/**
|
|
4
|
+
* Row type from PostgreSQL query
|
|
5
|
+
*/
|
|
6
|
+
export interface DutyRow {
|
|
7
|
+
validator_address: string;
|
|
8
|
+
slot: string;
|
|
9
|
+
block_number: string;
|
|
10
|
+
duty_type: DutyType;
|
|
11
|
+
status: DutyStatus;
|
|
12
|
+
message_hash: string;
|
|
13
|
+
signature: string | null;
|
|
14
|
+
node_id: string;
|
|
15
|
+
lock_token: string;
|
|
16
|
+
started_at: Date;
|
|
17
|
+
completed_at: Date | null;
|
|
18
|
+
error_message: string | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
|
|
22
|
+
*/
|
|
23
|
+
export interface InsertOrGetRow extends DutyRow {
|
|
24
|
+
is_new: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Type of validator duty being performed
|
|
28
|
+
*/
|
|
29
|
+
export declare enum DutyType {
|
|
30
|
+
BLOCK_PROPOSAL = "BLOCK_PROPOSAL",
|
|
31
|
+
ATTESTATION = "ATTESTATION",
|
|
32
|
+
ATTESTATIONS_AND_SIGNERS = "ATTESTATIONS_AND_SIGNERS"
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Status of a duty in the database
|
|
36
|
+
*/
|
|
37
|
+
export declare enum DutyStatus {
|
|
38
|
+
SIGNING = "signing",
|
|
39
|
+
SIGNED = "signed"
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Record of a validator duty in the database
|
|
43
|
+
*/
|
|
44
|
+
export interface ValidatorDutyRecord {
|
|
45
|
+
/** Ethereum address of the validator */
|
|
46
|
+
validatorAddress: EthAddress;
|
|
47
|
+
/** Slot number for this duty */
|
|
48
|
+
slot: bigint;
|
|
49
|
+
/** Block number for this duty */
|
|
50
|
+
blockNumber: bigint;
|
|
51
|
+
/** Type of duty being performed */
|
|
52
|
+
dutyType: DutyType;
|
|
53
|
+
/** Current status of the duty */
|
|
54
|
+
status: DutyStatus;
|
|
55
|
+
/** The signing root (hash) for this duty */
|
|
56
|
+
messageHash: string;
|
|
57
|
+
/** The signature (populated after successful signing) */
|
|
58
|
+
signature?: string;
|
|
59
|
+
/** Unique identifier for the node that acquired the lock */
|
|
60
|
+
nodeId: string;
|
|
61
|
+
/** Secret token for verifying ownership of the duty lock */
|
|
62
|
+
lockToken: string;
|
|
63
|
+
/** When the duty signing was started */
|
|
64
|
+
startedAt: Date;
|
|
65
|
+
/** When the duty signing was completed (success or failure) */
|
|
66
|
+
completedAt?: Date;
|
|
67
|
+
/** Error message if status is 'failed' */
|
|
68
|
+
errorMessage?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Minimal info needed to identify a unique duty
|
|
72
|
+
*/
|
|
73
|
+
export interface DutyIdentifier {
|
|
74
|
+
validatorAddress: EthAddress;
|
|
75
|
+
slot: bigint;
|
|
76
|
+
dutyType: DutyType;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parameters for checking and recording a new duty
|
|
80
|
+
*/
|
|
81
|
+
export interface CheckAndRecordParams {
|
|
82
|
+
validatorAddress: EthAddress;
|
|
83
|
+
slot: bigint;
|
|
84
|
+
blockNumber: bigint;
|
|
85
|
+
dutyType: DutyType;
|
|
86
|
+
messageHash: string;
|
|
87
|
+
nodeId: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Parameters for recording a successful signing
|
|
91
|
+
*/
|
|
92
|
+
export interface RecordSuccessParams {
|
|
93
|
+
validatorAddress: EthAddress;
|
|
94
|
+
slot: bigint;
|
|
95
|
+
dutyType: DutyType;
|
|
96
|
+
signature: Signature;
|
|
97
|
+
nodeId: string;
|
|
98
|
+
lockToken: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Parameters for deleting a duty
|
|
102
|
+
*/
|
|
103
|
+
export interface DeleteDutyParams {
|
|
104
|
+
validatorAddress: EthAddress;
|
|
105
|
+
slot: bigint;
|
|
106
|
+
dutyType: DutyType;
|
|
107
|
+
lockToken: string;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2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ACpB,QAAQ,EAAE,QAAQ,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,gBAAgB,EAAE,UAAU,CAAC;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,SAAS,EAAE,SAAS,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,UAAU,CAAC;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
package/dest/db/types.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type of validator duty being performed
|
|
3
|
+
*/ export var DutyType = /*#__PURE__*/ function(DutyType) {
|
|
4
|
+
DutyType["BLOCK_PROPOSAL"] = "BLOCK_PROPOSAL";
|
|
5
|
+
DutyType["ATTESTATION"] = "ATTESTATION";
|
|
6
|
+
DutyType["ATTESTATIONS_AND_SIGNERS"] = "ATTESTATIONS_AND_SIGNERS";
|
|
7
|
+
return DutyType;
|
|
8
|
+
}({});
|
|
9
|
+
/**
|
|
10
|
+
* Status of a duty in the database
|
|
11
|
+
*/ export var DutyStatus = /*#__PURE__*/ function(DutyStatus) {
|
|
12
|
+
DutyStatus["SIGNING"] = "signing";
|
|
13
|
+
DutyStatus["SIGNED"] = "signed";
|
|
14
|
+
return DutyStatus;
|
|
15
|
+
}({});
|
package/dest/errors.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom errors for the validator HA signer
|
|
3
|
+
*/
|
|
4
|
+
import type { DutyType } from './db/types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Thrown when a duty has already been signed (by any node).
|
|
7
|
+
* This is expected behavior in an HA setup - all nodes try to sign,
|
|
8
|
+
* the first one wins, and subsequent attempts get this error.
|
|
9
|
+
*/
|
|
10
|
+
export declare class DutyAlreadySignedError extends Error {
|
|
11
|
+
readonly slot: bigint;
|
|
12
|
+
readonly dutyType: DutyType;
|
|
13
|
+
readonly signedByNode: string;
|
|
14
|
+
constructor(slot: bigint, dutyType: DutyType, signedByNode: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Thrown when attempting to sign data that conflicts with an already-signed duty.
|
|
18
|
+
* This means the same validator tried to sign DIFFERENT data for the same slot.
|
|
19
|
+
*
|
|
20
|
+
* This is expected in HA setups where nodes may build different blocks
|
|
21
|
+
* (e.g., different transaction ordering) - the protection prevents double-signing.
|
|
22
|
+
*/
|
|
23
|
+
export declare class SlashingProtectionError extends Error {
|
|
24
|
+
readonly slot: bigint;
|
|
25
|
+
readonly dutyType: DutyType;
|
|
26
|
+
readonly existingMessageHash: string;
|
|
27
|
+
readonly attemptedMessageHash: string;
|
|
28
|
+
constructor(slot: bigint, dutyType: DutyType, existingMessageHash: string, attemptedMessageHash: string);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXJyb3JzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZXJyb3JzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsT0FBTyxLQUFLLEVBQUUsUUFBUSxFQUFFLE1BQU0sZUFBZSxDQUFDO0FBRTlDOzs7O0dBSUc7QUFDSCxxQkFBYSxzQkFBdUIsU0FBUSxLQUFLO2FBRTdCLElBQUksRUFBRSxNQUFNO2FBQ1osUUFBUSxFQUFFLFFBQVE7YUFDbEIsWUFBWSxFQUFFLE1BQU07SUFIdEMsWUFDa0IsSUFBSSxFQUFFLE1BQU0sRUFDWixRQUFRLEVBQUUsUUFBUSxFQUNsQixZQUFZLEVBQUUsTUFBTSxFQUlyQztDQUNGO0FBRUQ7Ozs7OztHQU1HO0FBQ0gscUJBQWEsdUJBQXdCLFNBQVEsS0FBSzthQUU5QixJQUFJLEVBQUUsTUFBTTthQUNaLFFBQVEsRUFBRSxRQUFRO2FBQ2xCLG1CQUFtQixFQUFFLE1BQU07YUFDM0Isb0JBQW9CLEVBQUUsTUFBTTtJQUo5QyxZQUNrQixJQUFJLEVBQUUsTUFBTSxFQUNaLFFBQVEsRUFBRSxRQUFRLEVBQ2xCLG1CQUFtQixFQUFFLE1BQU0sRUFDM0Isb0JBQW9CLEVBQUUsTUFBTSxFQU83QztDQUNGIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;GAIG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;aAE7B,IAAI,EAAE,MAAM;aACZ,QAAQ,EAAE,QAAQ;aAClB,YAAY,EAAE,MAAM;IAHtC,YACkB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,YAAY,EAAE,MAAM,EAIrC;CACF;AAED;;;;;;GAMG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;aAE9B,IAAI,EAAE,MAAM;aACZ,QAAQ,EAAE,QAAQ;aAClB,mBAAmB,EAAE,MAAM;aAC3B,oBAAoB,EAAE,MAAM;IAJ9C,YACkB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,mBAAmB,EAAE,MAAM,EAC3B,oBAAoB,EAAE,MAAM,EAO7C;CACF"}
|
package/dest/errors.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom errors for the validator HA signer
|
|
3
|
+
*/ /**
|
|
4
|
+
* Thrown when a duty has already been signed (by any node).
|
|
5
|
+
* This is expected behavior in an HA setup - all nodes try to sign,
|
|
6
|
+
* the first one wins, and subsequent attempts get this error.
|
|
7
|
+
*/ export class DutyAlreadySignedError extends Error {
|
|
8
|
+
slot;
|
|
9
|
+
dutyType;
|
|
10
|
+
signedByNode;
|
|
11
|
+
constructor(slot, dutyType, signedByNode){
|
|
12
|
+
super(`Duty ${dutyType} for slot ${slot} already signed by node ${signedByNode}`), this.slot = slot, this.dutyType = dutyType, this.signedByNode = signedByNode;
|
|
13
|
+
this.name = 'DutyAlreadySignedError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Thrown when attempting to sign data that conflicts with an already-signed duty.
|
|
18
|
+
* This means the same validator tried to sign DIFFERENT data for the same slot.
|
|
19
|
+
*
|
|
20
|
+
* This is expected in HA setups where nodes may build different blocks
|
|
21
|
+
* (e.g., different transaction ordering) - the protection prevents double-signing.
|
|
22
|
+
*/ export class SlashingProtectionError extends Error {
|
|
23
|
+
slot;
|
|
24
|
+
dutyType;
|
|
25
|
+
existingMessageHash;
|
|
26
|
+
attemptedMessageHash;
|
|
27
|
+
constructor(slot, dutyType, existingMessageHash, attemptedMessageHash){
|
|
28
|
+
super(`Slashing protection: ${dutyType} for slot ${slot} was already signed with different data. ` + `Existing: ${existingMessageHash.slice(0, 10)}..., Attempted: ${attemptedMessageHash.slice(0, 10)}...`), this.slot = slot, this.dutyType = dutyType, this.existingMessageHash = existingMessageHash, this.attemptedMessageHash = attemptedMessageHash;
|
|
29
|
+
this.name = 'SlashingProtectionError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { CreateHASignerConfig } from './config.js';
|
|
2
|
+
import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
|
|
3
|
+
import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
4
|
+
/**
|
|
5
|
+
* Create a validator HA signer with PostgreSQL backend
|
|
6
|
+
*
|
|
7
|
+
* After creating the signer, call `signer.start()` to begin background
|
|
8
|
+
* cleanup tasks. Call `signer.stop()` during graceful shutdown.
|
|
9
|
+
*
|
|
10
|
+
* Example with manual migrations (recommended for production):
|
|
11
|
+
* ```bash
|
|
12
|
+
* # Run migrations separately
|
|
13
|
+
* yarn migrate:up
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const { signer, db } = await createHASigner({
|
|
18
|
+
* databaseUrl: process.env.DATABASE_URL,
|
|
19
|
+
* enabled: true,
|
|
20
|
+
* nodeId: 'validator-node-1',
|
|
21
|
+
* pollingIntervalMs: 100,
|
|
22
|
+
* signingTimeoutMs: 3000,
|
|
23
|
+
* });
|
|
24
|
+
* signer.start(); // Start background cleanup
|
|
25
|
+
*
|
|
26
|
+
* // ... use signer ...
|
|
27
|
+
*
|
|
28
|
+
* await signer.stop(); // On shutdown
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Example with automatic migrations (simpler for dev/testing):
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const { signer, db } = await createHASigner({
|
|
34
|
+
* databaseUrl: process.env.DATABASE_URL,
|
|
35
|
+
* enabled: true,
|
|
36
|
+
* nodeId: 'validator-node-1',
|
|
37
|
+
* runMigrations: true, // Auto-run migrations on startup
|
|
38
|
+
* });
|
|
39
|
+
* signer.start();
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param config - Configuration for the HA signer
|
|
43
|
+
* @param deps - Optional dependencies (e.g., for testing)
|
|
44
|
+
* @returns An object containing the signer and database instances
|
|
45
|
+
*/
|
|
46
|
+
export declare function createHASigner(config: CreateHASignerConfig, deps?: CreateHASignerDeps): Promise<{
|
|
47
|
+
signer: ValidatorHASigner;
|
|
48
|
+
db: SlashingProtectionDatabase;
|
|
49
|
+
}>;
|
|
50
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIj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CAAC,CA4BD"}
|
package/dest/factory.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory functions for creating validator HA signers
|
|
3
|
+
*/ import { Pool } from 'pg';
|
|
4
|
+
import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
|
|
5
|
+
import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
6
|
+
/**
|
|
7
|
+
* Create a validator HA signer with PostgreSQL backend
|
|
8
|
+
*
|
|
9
|
+
* After creating the signer, call `signer.start()` to begin background
|
|
10
|
+
* cleanup tasks. Call `signer.stop()` during graceful shutdown.
|
|
11
|
+
*
|
|
12
|
+
* Example with manual migrations (recommended for production):
|
|
13
|
+
* ```bash
|
|
14
|
+
* # Run migrations separately
|
|
15
|
+
* yarn migrate:up
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const { signer, db } = await createHASigner({
|
|
20
|
+
* databaseUrl: process.env.DATABASE_URL,
|
|
21
|
+
* enabled: true,
|
|
22
|
+
* nodeId: 'validator-node-1',
|
|
23
|
+
* pollingIntervalMs: 100,
|
|
24
|
+
* signingTimeoutMs: 3000,
|
|
25
|
+
* });
|
|
26
|
+
* signer.start(); // Start background cleanup
|
|
27
|
+
*
|
|
28
|
+
* // ... use signer ...
|
|
29
|
+
*
|
|
30
|
+
* await signer.stop(); // On shutdown
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Example with automatic migrations (simpler for dev/testing):
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const { signer, db } = await createHASigner({
|
|
36
|
+
* databaseUrl: process.env.DATABASE_URL,
|
|
37
|
+
* enabled: true,
|
|
38
|
+
* nodeId: 'validator-node-1',
|
|
39
|
+
* runMigrations: true, // Auto-run migrations on startup
|
|
40
|
+
* });
|
|
41
|
+
* signer.start();
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param config - Configuration for the HA signer
|
|
45
|
+
* @param deps - Optional dependencies (e.g., for testing)
|
|
46
|
+
* @returns An object containing the signer and database instances
|
|
47
|
+
*/ export async function createHASigner(config, deps) {
|
|
48
|
+
const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } = config;
|
|
49
|
+
// Create connection pool (or use provided pool)
|
|
50
|
+
let pool;
|
|
51
|
+
if (!deps?.pool) {
|
|
52
|
+
pool = new Pool({
|
|
53
|
+
connectionString: databaseUrl,
|
|
54
|
+
max: poolMaxCount ?? 10,
|
|
55
|
+
min: poolMinCount ?? 0,
|
|
56
|
+
idleTimeoutMillis: poolIdleTimeoutMs ?? 10_000,
|
|
57
|
+
connectionTimeoutMillis: poolConnectionTimeoutMs ?? 0
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
pool = deps.pool;
|
|
61
|
+
}
|
|
62
|
+
// Create database instance
|
|
63
|
+
const db = new PostgresSlashingProtectionDatabase(pool);
|
|
64
|
+
// Verify database schema is initialized and version matches
|
|
65
|
+
await db.initialize();
|
|
66
|
+
// Create signer
|
|
67
|
+
const signer = new ValidatorHASigner(db, {
|
|
68
|
+
...signerConfig,
|
|
69
|
+
databaseUrl
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
signer,
|
|
73
|
+
db
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface RunMigrationsOptions {
|
|
2
|
+
/** Migration direction ('up' to apply, 'down' to rollback). Defaults to 'up'. */
|
|
3
|
+
direction?: 'up' | 'down';
|
|
4
|
+
/** Enable verbose output. Defaults to false. */
|
|
5
|
+
verbose?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Run database migrations programmatically
|
|
9
|
+
*
|
|
10
|
+
* @param databaseUrl - PostgreSQL connection string
|
|
11
|
+
* @param options - Migration options (direction, verbose)
|
|
12
|
+
* @returns Array of applied migration names
|
|
13
|
+
*/
|
|
14
|
+
export declare function runMigrations(databaseUrl: string, options?: RunMigrationsOptions): Promise<string[]>;
|
|
15
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWlncmF0aW9ucy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL21pZ3JhdGlvbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBWUEsTUFBTSxXQUFXLG9CQUFvQjtJQUNuQyxpRkFBaUY7SUFDakYsU0FBUyxDQUFDLEVBQUUsSUFBSSxHQUFHLE1BQU0sQ0FBQztJQUMxQixnREFBZ0Q7SUFDaEQsT0FBTyxDQUFDLEVBQUUsT0FBTyxDQUFDO0NBQ25CO0FBRUQ7Ozs7OztHQU1HO0FBQ0gsd0JBQXNCLGFBQWEsQ0FBQyxXQUFXLEVBQUUsTUFBTSxFQUFFLE9BQU8sR0FBRSxvQkFBeUIsR0FBRyxPQUFPLENBQUMsTUFBTSxFQUFFLENBQUMsQ0FnQzlHIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../src/migrations.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,oBAAoB;IACnC,iFAAiF;IACjF,SAAS,CAAC,EAAE,IAAI,GAAG,MAAM,CAAC;IAC1B,gDAAgD;IAChD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,oBAAyB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAgC9G"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Programmatic migration runner
|
|
3
|
+
*/ import { createLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { runner } from 'node-pg-migrate';
|
|
5
|
+
import { dirname, join } from 'path';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = dirname(__filename);
|
|
9
|
+
/**
|
|
10
|
+
* Run database migrations programmatically
|
|
11
|
+
*
|
|
12
|
+
* @param databaseUrl - PostgreSQL connection string
|
|
13
|
+
* @param options - Migration options (direction, verbose)
|
|
14
|
+
* @returns Array of applied migration names
|
|
15
|
+
*/ export async function runMigrations(databaseUrl, options = {}) {
|
|
16
|
+
const direction = options.direction ?? 'up';
|
|
17
|
+
const verbose = options.verbose ?? false;
|
|
18
|
+
const log = createLogger('validator-ha-signer:migrations');
|
|
19
|
+
try {
|
|
20
|
+
log.info(`Running migrations ${direction}...`);
|
|
21
|
+
const appliedMigrations = await runner({
|
|
22
|
+
databaseUrl,
|
|
23
|
+
dir: join(__dirname, 'db', 'migrations'),
|
|
24
|
+
direction,
|
|
25
|
+
migrationsTable: 'pgmigrations',
|
|
26
|
+
count: direction === 'down' ? 1 : Infinity,
|
|
27
|
+
verbose,
|
|
28
|
+
log: (msg)=>verbose ? log.info(msg) : log.debug(msg)
|
|
29
|
+
});
|
|
30
|
+
if (appliedMigrations.length === 0) {
|
|
31
|
+
log.info('No migrations to apply - schema is up to date');
|
|
32
|
+
} else {
|
|
33
|
+
log.info(`Applied ${appliedMigrations.length} migration(s)`, {
|
|
34
|
+
migrations: appliedMigrations.map((m)=>m.name)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return appliedMigrations.map((m)=>m.name);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
log.error('Migration failed', error);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|