@aztec/validator-ha-signer 0.0.1-commit.96bb3f7 → 0.0.1-commit.d1f2d6c
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 +42 -37
- package/dest/config.d.ts +49 -17
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +28 -19
- package/dest/db/postgres.d.ts +20 -5
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +50 -19
- package/dest/db/schema.d.ts +11 -7
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +19 -7
- package/dest/db/types.d.ts +75 -23
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +34 -0
- 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 +6 -14
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +6 -11
- 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 +9 -3
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +21 -9
- 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 +78 -14
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +21 -1
- package/dest/validator_ha_signer.d.ts +7 -11
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +25 -29
- package/package.json +8 -8
- package/src/config.ts +59 -50
- package/src/db/postgres.ts +68 -19
- package/src/db/schema.ts +19 -7
- package/src/db/types.ts +105 -21
- package/src/errors.ts +7 -2
- package/src/factory.ts +8 -13
- package/src/migrations.ts +17 -1
- package/src/slashing_protection_service.ts +46 -12
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +121 -19
- package/src/validator_ha_signer.ts +32 -39
package/src/db/types.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
|
|
1
2
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
3
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
3
4
|
|
|
@@ -8,6 +9,7 @@ export interface DutyRow {
|
|
|
8
9
|
validator_address: string;
|
|
9
10
|
slot: string;
|
|
10
11
|
block_number: string;
|
|
12
|
+
block_index_within_checkpoint: number;
|
|
11
13
|
duty_type: DutyType;
|
|
12
14
|
status: DutyStatus;
|
|
13
15
|
message_hash: string;
|
|
@@ -31,8 +33,13 @@ export interface InsertOrGetRow extends DutyRow {
|
|
|
31
33
|
*/
|
|
32
34
|
export enum DutyType {
|
|
33
35
|
BLOCK_PROPOSAL = 'BLOCK_PROPOSAL',
|
|
36
|
+
CHECKPOINT_PROPOSAL = 'CHECKPOINT_PROPOSAL',
|
|
34
37
|
ATTESTATION = 'ATTESTATION',
|
|
35
38
|
ATTESTATIONS_AND_SIGNERS = 'ATTESTATIONS_AND_SIGNERS',
|
|
39
|
+
GOVERNANCE_VOTE = 'GOVERNANCE_VOTE',
|
|
40
|
+
SLASHING_VOTE = 'SLASHING_VOTE',
|
|
41
|
+
AUTH_REQUEST = 'AUTH_REQUEST',
|
|
42
|
+
TXS = 'TXS',
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
/**
|
|
@@ -50,9 +57,11 @@ export interface ValidatorDutyRecord {
|
|
|
50
57
|
/** Ethereum address of the validator */
|
|
51
58
|
validatorAddress: EthAddress;
|
|
52
59
|
/** Slot number for this duty */
|
|
53
|
-
slot:
|
|
60
|
+
slot: SlotNumber;
|
|
54
61
|
/** Block number for this duty */
|
|
55
|
-
blockNumber:
|
|
62
|
+
blockNumber: BlockNumber;
|
|
63
|
+
/** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
|
|
64
|
+
blockIndexWithinCheckpoint: number;
|
|
56
65
|
/** Type of duty being performed */
|
|
57
66
|
dutyType: DutyType;
|
|
58
67
|
/** Current status of the duty */
|
|
@@ -74,44 +83,119 @@ export interface ValidatorDutyRecord {
|
|
|
74
83
|
}
|
|
75
84
|
|
|
76
85
|
/**
|
|
77
|
-
*
|
|
86
|
+
* Duty identifier for block proposals.
|
|
87
|
+
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
78
88
|
*/
|
|
79
|
-
export interface
|
|
89
|
+
export interface BlockProposalDutyIdentifier {
|
|
80
90
|
validatorAddress: EthAddress;
|
|
81
|
-
slot:
|
|
82
|
-
|
|
91
|
+
slot: SlotNumber;
|
|
92
|
+
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
93
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint;
|
|
94
|
+
dutyType: DutyType.BLOCK_PROPOSAL;
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
/**
|
|
86
|
-
*
|
|
98
|
+
* Duty identifier for non-block-proposal duties.
|
|
99
|
+
* blockIndexWithinCheckpoint is not applicable (internally stored as -1).
|
|
87
100
|
*/
|
|
88
|
-
export interface
|
|
101
|
+
export interface OtherDutyIdentifier {
|
|
89
102
|
validatorAddress: EthAddress;
|
|
90
|
-
slot:
|
|
91
|
-
|
|
92
|
-
|
|
103
|
+
slot: SlotNumber;
|
|
104
|
+
dutyType:
|
|
105
|
+
| DutyType.CHECKPOINT_PROPOSAL
|
|
106
|
+
| DutyType.ATTESTATION
|
|
107
|
+
| DutyType.ATTESTATIONS_AND_SIGNERS
|
|
108
|
+
| DutyType.GOVERNANCE_VOTE
|
|
109
|
+
| DutyType.SLASHING_VOTE
|
|
110
|
+
| DutyType.AUTH_REQUEST
|
|
111
|
+
| DutyType.TXS;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Minimal info needed to identify a unique duty.
|
|
116
|
+
* Uses discriminated union to enforce type safety:
|
|
117
|
+
* - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
|
|
118
|
+
* - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
|
|
119
|
+
*/
|
|
120
|
+
export type DutyIdentifier = BlockProposalDutyIdentifier | OtherDutyIdentifier;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Validates and normalizes the block index for a duty.
|
|
124
|
+
* - BLOCK_PROPOSAL: validates blockIndexWithinCheckpoint is provided and >= 0
|
|
125
|
+
* - Other duty types: always returns -1
|
|
126
|
+
*
|
|
127
|
+
* @throws Error if BLOCK_PROPOSAL is missing blockIndexWithinCheckpoint or has invalid value
|
|
128
|
+
*/
|
|
129
|
+
export function normalizeBlockIndex(dutyType: DutyType, blockIndexWithinCheckpoint: number | undefined): number {
|
|
130
|
+
if (dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
131
|
+
if (blockIndexWithinCheckpoint === undefined) {
|
|
132
|
+
throw new Error('BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint to be specified');
|
|
133
|
+
}
|
|
134
|
+
if (blockIndexWithinCheckpoint < 0) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint >= 0, got ${blockIndexWithinCheckpoint}`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
return blockIndexWithinCheckpoint;
|
|
140
|
+
}
|
|
141
|
+
// For all other duty types, always use -1
|
|
142
|
+
return -1;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Gets the block index from a DutyIdentifier.
|
|
147
|
+
* - BLOCK_PROPOSAL: returns the blockIndexWithinCheckpoint
|
|
148
|
+
* - Other duty types: returns -1
|
|
149
|
+
*/
|
|
150
|
+
export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
|
|
151
|
+
if (duty.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
152
|
+
return duty.blockIndexWithinCheckpoint;
|
|
153
|
+
}
|
|
154
|
+
return -1;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Additional parameters for checking and recording a new duty
|
|
159
|
+
*/
|
|
160
|
+
interface CheckAndRecordExtra {
|
|
161
|
+
/** Block number for this duty */
|
|
162
|
+
blockNumber: BlockNumber | CheckpointNumber;
|
|
163
|
+
/** The signing root (hash) for this duty */
|
|
93
164
|
messageHash: string;
|
|
165
|
+
/** Identifier for the node that acquired the lock */
|
|
94
166
|
nodeId: string;
|
|
95
167
|
}
|
|
96
168
|
|
|
97
169
|
/**
|
|
98
|
-
* Parameters for recording a
|
|
170
|
+
* Parameters for checking and recording a new duty.
|
|
171
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
99
172
|
*/
|
|
100
|
-
export
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
173
|
+
export type CheckAndRecordParams = DutyIdentifier & CheckAndRecordExtra;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Additional parameters for recording a successful signing
|
|
177
|
+
*/
|
|
178
|
+
interface RecordSuccessExtra {
|
|
104
179
|
signature: Signature;
|
|
105
180
|
nodeId: string;
|
|
106
181
|
lockToken: string;
|
|
107
182
|
}
|
|
108
183
|
|
|
109
184
|
/**
|
|
110
|
-
* Parameters for
|
|
185
|
+
* Parameters for recording a successful signing.
|
|
186
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
111
187
|
*/
|
|
112
|
-
export
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
188
|
+
export type RecordSuccessParams = DutyIdentifier & RecordSuccessExtra;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Additional parameters for deleting a duty
|
|
192
|
+
*/
|
|
193
|
+
interface DeleteDutyExtra {
|
|
116
194
|
lockToken: string;
|
|
117
195
|
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Parameters for deleting a duty.
|
|
199
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
200
|
+
*/
|
|
201
|
+
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
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { Pool } from 'pg';
|
|
5
5
|
|
|
6
|
-
import type {
|
|
6
|
+
import type { ValidatorHASignerConfig } from './config.js';
|
|
7
7
|
import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
|
|
8
8
|
import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
|
|
9
9
|
import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
@@ -23,7 +23,7 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
|
23
23
|
* ```typescript
|
|
24
24
|
* const { signer, db } = await createHASigner({
|
|
25
25
|
* databaseUrl: process.env.DATABASE_URL,
|
|
26
|
-
*
|
|
26
|
+
* haSigningEnabled: true,
|
|
27
27
|
* nodeId: 'validator-node-1',
|
|
28
28
|
* pollingIntervalMs: 100,
|
|
29
29
|
* signingTimeoutMs: 3000,
|
|
@@ -35,23 +35,15 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
|
|
|
35
35
|
* await signer.stop(); // On shutdown
|
|
36
36
|
* ```
|
|
37
37
|
*
|
|
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
|
-
* ```
|
|
38
|
+
* Note: Migrations must be run separately using `aztec migrate-ha-db up` before
|
|
39
|
+
* creating the signer. The factory will verify the schema is initialized via `db.initialize()`.
|
|
48
40
|
*
|
|
49
41
|
* @param config - Configuration for the HA signer
|
|
50
42
|
* @param deps - Optional dependencies (e.g., for testing)
|
|
51
43
|
* @returns An object containing the signer and database instances
|
|
52
44
|
*/
|
|
53
45
|
export async function createHASigner(
|
|
54
|
-
config:
|
|
46
|
+
config: ValidatorHASignerConfig,
|
|
55
47
|
deps?: CreateHASignerDeps,
|
|
56
48
|
): Promise<{
|
|
57
49
|
signer: ValidatorHASigner;
|
|
@@ -60,6 +52,9 @@ export async function createHASigner(
|
|
|
60
52
|
const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } =
|
|
61
53
|
config;
|
|
62
54
|
|
|
55
|
+
if (!databaseUrl) {
|
|
56
|
+
throw new Error('databaseUrl is required for createHASigner');
|
|
57
|
+
}
|
|
63
58
|
// Create connection pool (or use provided pool)
|
|
64
59
|
let pool: Pool;
|
|
65
60
|
if (!deps?.pool) {
|
package/src/migrations.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
5
5
|
|
|
6
|
+
import { readdirSync } from 'fs';
|
|
6
7
|
import { runner } from 'node-pg-migrate';
|
|
7
8
|
import { dirname, join } from 'path';
|
|
8
9
|
import { fileURLToPath } from 'url';
|
|
@@ -30,17 +31,32 @@ export async function runMigrations(databaseUrl: string, options: RunMigrationsO
|
|
|
30
31
|
|
|
31
32
|
const log = createLogger('validator-ha-signer:migrations');
|
|
32
33
|
|
|
34
|
+
const migrationsDir = join(__dirname, 'db', 'migrations');
|
|
35
|
+
|
|
33
36
|
try {
|
|
34
37
|
log.info(`Running migrations ${direction}...`);
|
|
35
38
|
|
|
39
|
+
// Filter out .d.ts and .d.ts.map files - node-pg-migrate only needs .js files
|
|
40
|
+
const migrationFiles = readdirSync(migrationsDir);
|
|
41
|
+
const jsMigrationFiles = migrationFiles.filter(
|
|
42
|
+
file => file.endsWith('.js') && !file.endsWith('.d.ts') && !file.endsWith('.d.ts.map'),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
if (jsMigrationFiles.length === 0) {
|
|
46
|
+
log.info('No migration files found');
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
const appliedMigrations = await runner({
|
|
37
51
|
databaseUrl,
|
|
38
|
-
dir:
|
|
52
|
+
dir: migrationsDir,
|
|
39
53
|
direction,
|
|
40
54
|
migrationsTable: 'pgmigrations',
|
|
41
55
|
count: direction === 'down' ? 1 : Infinity,
|
|
42
56
|
verbose,
|
|
43
57
|
log: msg => (verbose ? log.info(msg) : log.debug(msg)),
|
|
58
|
+
// Ignore TypeScript declaration files - node-pg-migrate will try to import them otherwise
|
|
59
|
+
ignorePattern: '.*\\.d\\.(ts|js)$|.*\\.d\\.ts\\.map$',
|
|
44
60
|
});
|
|
45
61
|
|
|
46
62
|
if (appliedMigrations.length === 0) {
|
|
@@ -8,9 +8,15 @@ 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
|
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
type CheckAndRecordParams,
|
|
13
|
+
type DeleteDutyParams,
|
|
14
|
+
DutyStatus,
|
|
15
|
+
type RecordSuccessParams,
|
|
16
|
+
getBlockIndexFromDutyIdentifier,
|
|
17
|
+
} from './db/types.js';
|
|
12
18
|
import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
13
|
-
import type {
|
|
19
|
+
import type { SlashingProtectionDatabase, ValidatorHASignerConfig } from './types.js';
|
|
14
20
|
|
|
15
21
|
/**
|
|
16
22
|
* Slashing Protection Service
|
|
@@ -31,21 +37,24 @@ export class SlashingProtectionService {
|
|
|
31
37
|
private readonly log: Logger;
|
|
32
38
|
private readonly pollingIntervalMs: number;
|
|
33
39
|
private readonly signingTimeoutMs: number;
|
|
40
|
+
private readonly maxStuckDutiesAgeMs: number;
|
|
34
41
|
|
|
35
42
|
private cleanupRunningPromise: RunningPromise;
|
|
36
43
|
|
|
37
44
|
constructor(
|
|
38
45
|
private readonly db: SlashingProtectionDatabase,
|
|
39
|
-
private readonly config:
|
|
46
|
+
private readonly config: ValidatorHASignerConfig,
|
|
40
47
|
) {
|
|
41
48
|
this.log = createLogger('slashing-protection');
|
|
42
49
|
this.pollingIntervalMs = config.pollingIntervalMs;
|
|
43
50
|
this.signingTimeoutMs = config.signingTimeoutMs;
|
|
51
|
+
// Default to 144s (2x 72s Aztec slot duration) if not explicitly configured
|
|
52
|
+
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
44
53
|
|
|
45
54
|
this.cleanupRunningPromise = new RunningPromise(
|
|
46
55
|
this.cleanupStuckDuties.bind(this),
|
|
47
56
|
this.log,
|
|
48
|
-
this.
|
|
57
|
+
this.maxStuckDutiesAgeMs,
|
|
49
58
|
);
|
|
50
59
|
}
|
|
51
60
|
|
|
@@ -98,9 +107,16 @@ export class SlashingProtectionService {
|
|
|
98
107
|
existingNodeId: record.nodeId,
|
|
99
108
|
attemptingNodeId: nodeId,
|
|
100
109
|
});
|
|
101
|
-
throw new SlashingProtectionError(
|
|
110
|
+
throw new SlashingProtectionError(
|
|
111
|
+
slot,
|
|
112
|
+
dutyType,
|
|
113
|
+
record.blockIndexWithinCheckpoint,
|
|
114
|
+
record.messageHash,
|
|
115
|
+
messageHash,
|
|
116
|
+
record.nodeId,
|
|
117
|
+
);
|
|
102
118
|
}
|
|
103
|
-
throw new DutyAlreadySignedError(slot, dutyType, record.nodeId);
|
|
119
|
+
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
|
|
104
120
|
} else if (record.status === DutyStatus.SIGNING) {
|
|
105
121
|
// Another node is currently signing - check for timeout
|
|
106
122
|
if (Date.now() - startTime > this.signingTimeoutMs) {
|
|
@@ -109,7 +125,7 @@ export class SlashingProtectionService {
|
|
|
109
125
|
timeoutMs: this.signingTimeoutMs,
|
|
110
126
|
signingNodeId: record.nodeId,
|
|
111
127
|
});
|
|
112
|
-
throw new DutyAlreadySignedError(slot, dutyType, 'unknown (timeout)');
|
|
128
|
+
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, 'unknown (timeout)');
|
|
113
129
|
}
|
|
114
130
|
|
|
115
131
|
// Wait and poll
|
|
@@ -134,8 +150,16 @@ export class SlashingProtectionService {
|
|
|
134
150
|
*/
|
|
135
151
|
async recordSuccess(params: RecordSuccessParams): Promise<boolean> {
|
|
136
152
|
const { validatorAddress, slot, dutyType, signature, nodeId, lockToken } = params;
|
|
137
|
-
|
|
138
|
-
|
|
153
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
154
|
+
|
|
155
|
+
const success = await this.db.updateDutySigned(
|
|
156
|
+
validatorAddress,
|
|
157
|
+
slot,
|
|
158
|
+
dutyType,
|
|
159
|
+
signature.toString(),
|
|
160
|
+
lockToken,
|
|
161
|
+
blockIndexWithinCheckpoint,
|
|
162
|
+
);
|
|
139
163
|
|
|
140
164
|
if (success) {
|
|
141
165
|
this.log.info(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
|
|
@@ -161,8 +185,9 @@ export class SlashingProtectionService {
|
|
|
161
185
|
*/
|
|
162
186
|
async deleteDuty(params: DeleteDutyParams): Promise<boolean> {
|
|
163
187
|
const { validatorAddress, slot, dutyType, lockToken } = params;
|
|
188
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
164
189
|
|
|
165
|
-
const success = await this.db.deleteDuty(validatorAddress, slot, dutyType, lockToken);
|
|
190
|
+
const success = await this.db.deleteDuty(validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint);
|
|
166
191
|
|
|
167
192
|
if (success) {
|
|
168
193
|
this.log.info(`Deleted duty ${dutyType} at slot ${slot} to allow retry`, {
|
|
@@ -201,15 +226,24 @@ export class SlashingProtectionService {
|
|
|
201
226
|
this.log.info('Slashing protection service stopped', { nodeId: this.config.nodeId });
|
|
202
227
|
}
|
|
203
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Close the database connection.
|
|
231
|
+
* Should be called after stop() during graceful shutdown.
|
|
232
|
+
*/
|
|
233
|
+
async close() {
|
|
234
|
+
await this.db.close();
|
|
235
|
+
this.log.info('Slashing protection database connection closed');
|
|
236
|
+
}
|
|
237
|
+
|
|
204
238
|
/**
|
|
205
239
|
* Cleanup own stuck duties
|
|
206
240
|
*/
|
|
207
241
|
private async cleanupStuckDuties() {
|
|
208
|
-
const numDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.
|
|
242
|
+
const numDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
|
|
209
243
|
if (numDuties > 0) {
|
|
210
244
|
this.log.info(`Cleaned up ${numDuties} stuck duties`, {
|
|
211
245
|
nodeId: this.config.nodeId,
|
|
212
|
-
maxStuckDutiesAgeMs: this.
|
|
246
|
+
maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs,
|
|
213
247
|
});
|
|
214
248
|
}
|
|
215
249
|
}
|
|
@@ -0,0 +1,256 @@
|
|
|
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
|
+
*/
|
|
13
|
+
import type { PGliteInterface } from '@electric-sql/pglite';
|
|
14
|
+
import { EventEmitter } from 'events';
|
|
15
|
+
import type { QueryResult, QueryResultRow } from 'pg';
|
|
16
|
+
import { Readable, Writable } from 'stream';
|
|
17
|
+
|
|
18
|
+
export interface PoolConfig {
|
|
19
|
+
pglite: PGliteInterface;
|
|
20
|
+
max?: number;
|
|
21
|
+
min?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface ClientConfig {
|
|
25
|
+
pglite: PGliteInterface;
|
|
26
|
+
host?: string;
|
|
27
|
+
port?: number;
|
|
28
|
+
ssl?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class Client extends EventEmitter {
|
|
32
|
+
protected pglite: PGliteInterface;
|
|
33
|
+
protected _connected = false;
|
|
34
|
+
readonly host: string;
|
|
35
|
+
readonly port: number;
|
|
36
|
+
readonly ssl: boolean;
|
|
37
|
+
readonly connection: object;
|
|
38
|
+
|
|
39
|
+
// Stub implementations for pg compatibility
|
|
40
|
+
readonly copyFrom = (): Writable => new Writable();
|
|
41
|
+
readonly copyTo = (): Readable => new Readable();
|
|
42
|
+
readonly pauseDrain = (): void => {};
|
|
43
|
+
readonly resumeDrain = (): void => {};
|
|
44
|
+
readonly escapeLiteral = (str: string): string => `'${str.replace(/'/g, "''")}'`;
|
|
45
|
+
readonly escapeIdentifier = (str: string): string => `"${str.replace(/"/g, '""')}"`;
|
|
46
|
+
readonly setTypeParser = (): void => {};
|
|
47
|
+
readonly getTypeParser = (): ((value: string) => unknown) => (value: string) => value;
|
|
48
|
+
|
|
49
|
+
constructor(config: ClientConfig) {
|
|
50
|
+
super();
|
|
51
|
+
this.pglite = config.pglite;
|
|
52
|
+
this.host = config.host || 'localhost';
|
|
53
|
+
this.port = config.port || 5432;
|
|
54
|
+
this.ssl = typeof config.ssl === 'boolean' ? config.ssl : !!config.ssl;
|
|
55
|
+
this.connection = {};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
connect(): Promise<void> {
|
|
59
|
+
if (this._connected) {
|
|
60
|
+
return Promise.resolve();
|
|
61
|
+
}
|
|
62
|
+
this._connected = true;
|
|
63
|
+
this.emit('connect');
|
|
64
|
+
return Promise.resolve();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
end(): Promise<void> {
|
|
68
|
+
if (!this._connected) {
|
|
69
|
+
return Promise.resolve();
|
|
70
|
+
}
|
|
71
|
+
this._connected = false;
|
|
72
|
+
this.emit('end');
|
|
73
|
+
return Promise.resolve();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>> {
|
|
77
|
+
if (!this._connected) {
|
|
78
|
+
throw new Error('Client is not connected');
|
|
79
|
+
}
|
|
80
|
+
const result = await this.pglite.query<R>(text, values);
|
|
81
|
+
return this.convertPGliteResult(result);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
protected convertPGliteResult<R extends QueryResultRow>(result: {
|
|
85
|
+
rows: R[];
|
|
86
|
+
fields: Array<{ name: string; dataTypeID: number }>;
|
|
87
|
+
affectedRows?: number;
|
|
88
|
+
}): QueryResult<R> {
|
|
89
|
+
return {
|
|
90
|
+
command: '',
|
|
91
|
+
rowCount: 'affectedRows' in result ? (result.affectedRows ?? 0) : result.rows.length,
|
|
92
|
+
oid: 0,
|
|
93
|
+
fields: result.fields.map(field => ({
|
|
94
|
+
name: field.name,
|
|
95
|
+
tableID: 0,
|
|
96
|
+
columnID: 0,
|
|
97
|
+
dataTypeID: field.dataTypeID,
|
|
98
|
+
dataTypeSize: -1,
|
|
99
|
+
dataTypeModifier: -1,
|
|
100
|
+
format: 'text',
|
|
101
|
+
})),
|
|
102
|
+
rows: result.rows,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get connected(): boolean {
|
|
107
|
+
return this._connected;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class Pool extends EventEmitter {
|
|
112
|
+
private clients: PoolClient[] = [];
|
|
113
|
+
private availableClients: PoolClient[] = [];
|
|
114
|
+
private waitingQueue: Array<(client: PoolClient) => void> = [];
|
|
115
|
+
private _ended = false;
|
|
116
|
+
private pglite: PGliteInterface;
|
|
117
|
+
private _config: PoolConfig;
|
|
118
|
+
|
|
119
|
+
readonly expiredCount = 0;
|
|
120
|
+
readonly options: PoolConfig;
|
|
121
|
+
|
|
122
|
+
constructor(config: PoolConfig) {
|
|
123
|
+
super();
|
|
124
|
+
this._config = { max: 10, min: 0, ...config };
|
|
125
|
+
this.pglite = config.pglite;
|
|
126
|
+
this.options = config;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get totalCount(): number {
|
|
130
|
+
return this.clients.length;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get idleCount(): number {
|
|
134
|
+
return this.availableClients.length;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get waitingCount(): number {
|
|
138
|
+
return this.waitingQueue.length;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get ending(): boolean {
|
|
142
|
+
return this._ended;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
get ended(): boolean {
|
|
146
|
+
return this._ended;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
connect(): Promise<PoolClient> {
|
|
150
|
+
if (this._ended) {
|
|
151
|
+
return Promise.reject(new Error('Pool is ended'));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (this.availableClients.length > 0) {
|
|
155
|
+
const client = this.availableClients.pop()!;
|
|
156
|
+
client._markInUse();
|
|
157
|
+
return Promise.resolve(client);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (this.clients.length < (this._config.max || 10)) {
|
|
161
|
+
const client = new PoolClient(this.pglite, this);
|
|
162
|
+
this.clients.push(client);
|
|
163
|
+
return Promise.resolve(client);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return new Promise(resolve => {
|
|
167
|
+
this.waitingQueue.push(resolve);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>> {
|
|
172
|
+
const client = await this.connect();
|
|
173
|
+
try {
|
|
174
|
+
return await client.query<R>(text, values);
|
|
175
|
+
} finally {
|
|
176
|
+
client.release();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
releaseClient(client: PoolClient): void {
|
|
181
|
+
const index = this.clients.indexOf(client);
|
|
182
|
+
if (index !== -1) {
|
|
183
|
+
client._markAvailable();
|
|
184
|
+
if (this.waitingQueue.length > 0) {
|
|
185
|
+
const resolve = this.waitingQueue.shift()!;
|
|
186
|
+
client._markInUse();
|
|
187
|
+
resolve(client);
|
|
188
|
+
} else {
|
|
189
|
+
this.availableClients.push(client);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
end(): Promise<void> {
|
|
195
|
+
this._ended = true;
|
|
196
|
+
this.clients.forEach(client => client._markReleased());
|
|
197
|
+
this.clients = [];
|
|
198
|
+
this.availableClients = [];
|
|
199
|
+
this.emit('end');
|
|
200
|
+
return Promise.resolve();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export class PoolClient extends Client {
|
|
205
|
+
private pool: Pool;
|
|
206
|
+
private _released = false;
|
|
207
|
+
private _inUse = true;
|
|
208
|
+
private _userReleased = false;
|
|
209
|
+
|
|
210
|
+
constructor(pglite: PGliteInterface, pool: Pool) {
|
|
211
|
+
super({ pglite });
|
|
212
|
+
this.pool = pool;
|
|
213
|
+
this._connected = true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
override async query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>> {
|
|
217
|
+
if (this._userReleased && !this._inUse) {
|
|
218
|
+
throw new Error('Client has been released back to the pool');
|
|
219
|
+
}
|
|
220
|
+
const result = await this.pglite.query<R>(text, values);
|
|
221
|
+
return this.convertPGliteResult(result);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
release(): void {
|
|
225
|
+
if (this._released || this._userReleased) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this._userReleased = true;
|
|
229
|
+
this.pool.releaseClient(this);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
override end(): Promise<void> {
|
|
233
|
+
this.release();
|
|
234
|
+
return Promise.resolve();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
_markInUse(): void {
|
|
238
|
+
this._inUse = true;
|
|
239
|
+
this._userReleased = false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_markAvailable(): void {
|
|
243
|
+
this._inUse = false;
|
|
244
|
+
this._userReleased = false;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
_markReleased(): void {
|
|
248
|
+
this._released = true;
|
|
249
|
+
this._inUse = false;
|
|
250
|
+
this._userReleased = true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
override get connected(): boolean {
|
|
254
|
+
return this._connected && !this._released;
|
|
255
|
+
}
|
|
256
|
+
}
|