@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/lmdb.ts
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LMDB implementation of SlashingProtectionDatabase
|
|
3
|
+
*
|
|
4
|
+
* Provides local (single-node) double-signing protection using LMDB as the backend.
|
|
5
|
+
* Suitable for nodes that do NOT run in a high-availability multi-node setup.
|
|
6
|
+
*
|
|
7
|
+
* The LMDB store is single-writer, making setIfNotExists inherently atomic.
|
|
8
|
+
* This means we get crash-restart protection without needing an external database.
|
|
9
|
+
*/
|
|
10
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
11
|
+
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
12
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
13
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
14
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
15
|
+
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
16
|
+
|
|
17
|
+
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
18
|
+
import {
|
|
19
|
+
type CheckAndRecordParams,
|
|
20
|
+
DutyStatus,
|
|
21
|
+
DutyType,
|
|
22
|
+
type StoredDutyRecord,
|
|
23
|
+
getBlockIndexFromDutyIdentifier,
|
|
24
|
+
recordFromFields,
|
|
25
|
+
} from './types.js';
|
|
26
|
+
|
|
27
|
+
function dutyKey(
|
|
28
|
+
rollupAddress: string,
|
|
29
|
+
validatorAddress: string,
|
|
30
|
+
slot: string,
|
|
31
|
+
dutyType: string,
|
|
32
|
+
blockIndexWithinCheckpoint: number,
|
|
33
|
+
): string {
|
|
34
|
+
return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* LMDB-backed implementation of SlashingProtectionDatabase.
|
|
39
|
+
*
|
|
40
|
+
* Provides single-node double-signing protection that survives crashes and restarts.
|
|
41
|
+
* Does not provide cross-node coordination (that requires the PostgreSQL implementation).
|
|
42
|
+
*/
|
|
43
|
+
export class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
44
|
+
public static readonly SCHEMA_VERSION = 2;
|
|
45
|
+
|
|
46
|
+
private readonly duties: AztecAsyncMap<string, StoredDutyRecord>;
|
|
47
|
+
private readonly log: Logger;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
private readonly store: AztecAsyncKVStore,
|
|
51
|
+
private readonly dateProvider: DateProvider,
|
|
52
|
+
) {
|
|
53
|
+
this.log = createLogger('slashing-protection:lmdb');
|
|
54
|
+
this.duties = store.openMap<string, StoredDutyRecord>('signing-protection-duties');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
59
|
+
*
|
|
60
|
+
* LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
|
|
61
|
+
*/
|
|
62
|
+
public async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
|
|
63
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
64
|
+
const key = dutyKey(
|
|
65
|
+
params.rollupAddress.toString(),
|
|
66
|
+
params.validatorAddress.toString(),
|
|
67
|
+
params.slot.toString(),
|
|
68
|
+
params.dutyType,
|
|
69
|
+
blockIndexWithinCheckpoint,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const lockToken = randomBytes(16).toString('hex');
|
|
73
|
+
const now = this.dateProvider.now();
|
|
74
|
+
|
|
75
|
+
const result = await this.store.transactionAsync(async () => {
|
|
76
|
+
const existing = await this.duties.getAsync(key);
|
|
77
|
+
if (existing) {
|
|
78
|
+
return { isNew: false as const, record: { ...existing, lockToken: '' } };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const newRecord: StoredDutyRecord = {
|
|
82
|
+
rollupAddress: params.rollupAddress.toString(),
|
|
83
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
84
|
+
slot: params.slot.toString(),
|
|
85
|
+
blockNumber: params.blockNumber.toString(),
|
|
86
|
+
checkpointNumber: params.checkpointNumber.toString(),
|
|
87
|
+
blockIndexWithinCheckpoint,
|
|
88
|
+
dutyType: params.dutyType,
|
|
89
|
+
status: DutyStatus.SIGNING,
|
|
90
|
+
messageHash: params.messageHash,
|
|
91
|
+
nodeId: params.nodeId,
|
|
92
|
+
lockToken,
|
|
93
|
+
startedAtMs: now,
|
|
94
|
+
};
|
|
95
|
+
await this.duties.set(key, newRecord);
|
|
96
|
+
return { isNew: true as const, record: newRecord };
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (result.isNew) {
|
|
100
|
+
this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
|
|
101
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
102
|
+
nodeId: params.nodeId,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { isNew: result.isNew, record: recordFromFields(result.record) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Update a duty to 'signed' status with the signature.
|
|
111
|
+
* Only succeeds if the lockToken matches.
|
|
112
|
+
*/
|
|
113
|
+
public updateDutySigned(
|
|
114
|
+
rollupAddress: EthAddress,
|
|
115
|
+
validatorAddress: EthAddress,
|
|
116
|
+
slot: SlotNumber,
|
|
117
|
+
dutyType: DutyType,
|
|
118
|
+
signature: string,
|
|
119
|
+
lockToken: string,
|
|
120
|
+
blockIndexWithinCheckpoint: number,
|
|
121
|
+
): Promise<boolean> {
|
|
122
|
+
const key = dutyKey(
|
|
123
|
+
rollupAddress.toString(),
|
|
124
|
+
validatorAddress.toString(),
|
|
125
|
+
slot.toString(),
|
|
126
|
+
dutyType,
|
|
127
|
+
blockIndexWithinCheckpoint,
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
return this.store.transactionAsync(async () => {
|
|
131
|
+
const existing = await this.duties.getAsync(key);
|
|
132
|
+
if (!existing) {
|
|
133
|
+
this.log.warn('Failed to update duty to signed: duty not found', {
|
|
134
|
+
rollupAddress: rollupAddress.toString(),
|
|
135
|
+
validatorAddress: validatorAddress.toString(),
|
|
136
|
+
slot: slot.toString(),
|
|
137
|
+
dutyType,
|
|
138
|
+
blockIndexWithinCheckpoint,
|
|
139
|
+
});
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (existing.lockToken !== lockToken) {
|
|
144
|
+
this.log.warn('Failed to update duty to signed: invalid token', {
|
|
145
|
+
rollupAddress: rollupAddress.toString(),
|
|
146
|
+
validatorAddress: validatorAddress.toString(),
|
|
147
|
+
slot: slot.toString(),
|
|
148
|
+
dutyType,
|
|
149
|
+
blockIndexWithinCheckpoint,
|
|
150
|
+
});
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
await this.duties.set(key, {
|
|
155
|
+
...existing,
|
|
156
|
+
status: DutyStatus.SIGNED,
|
|
157
|
+
signature,
|
|
158
|
+
completedAtMs: this.dateProvider.now(),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return true;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Delete a duty record.
|
|
167
|
+
* Only succeeds if the lockToken matches.
|
|
168
|
+
*/
|
|
169
|
+
public deleteDuty(
|
|
170
|
+
rollupAddress: EthAddress,
|
|
171
|
+
validatorAddress: EthAddress,
|
|
172
|
+
slot: SlotNumber,
|
|
173
|
+
dutyType: DutyType,
|
|
174
|
+
lockToken: string,
|
|
175
|
+
blockIndexWithinCheckpoint: number,
|
|
176
|
+
): Promise<boolean> {
|
|
177
|
+
const key = dutyKey(
|
|
178
|
+
rollupAddress.toString(),
|
|
179
|
+
validatorAddress.toString(),
|
|
180
|
+
slot.toString(),
|
|
181
|
+
dutyType,
|
|
182
|
+
blockIndexWithinCheckpoint,
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
return this.store.transactionAsync(async () => {
|
|
186
|
+
const existing = await this.duties.getAsync(key);
|
|
187
|
+
if (!existing || existing.lockToken !== lockToken) {
|
|
188
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
189
|
+
rollupAddress: rollupAddress.toString(),
|
|
190
|
+
validatorAddress: validatorAddress.toString(),
|
|
191
|
+
slot: slot.toString(),
|
|
192
|
+
dutyType,
|
|
193
|
+
blockIndexWithinCheckpoint,
|
|
194
|
+
});
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
await this.duties.delete(key);
|
|
199
|
+
return true;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
205
|
+
*/
|
|
206
|
+
public cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
207
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
208
|
+
|
|
209
|
+
return this.store.transactionAsync(async () => {
|
|
210
|
+
const keysToDelete: string[] = [];
|
|
211
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
212
|
+
if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
|
|
213
|
+
keysToDelete.push(key);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (const key of keysToDelete) {
|
|
217
|
+
await this.duties.delete(key);
|
|
218
|
+
}
|
|
219
|
+
return keysToDelete.length;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Cleanup duties with outdated rollup address.
|
|
225
|
+
*
|
|
226
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
227
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
228
|
+
* startup whenever the rollup address changes.
|
|
229
|
+
*/
|
|
230
|
+
public cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
|
|
231
|
+
return Promise.resolve(0);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
236
|
+
*/
|
|
237
|
+
public cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
238
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
239
|
+
|
|
240
|
+
return this.store.transactionAsync(async () => {
|
|
241
|
+
const keysToDelete: string[] = [];
|
|
242
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
243
|
+
if (
|
|
244
|
+
record.status === DutyStatus.SIGNED &&
|
|
245
|
+
record.completedAtMs !== undefined &&
|
|
246
|
+
record.completedAtMs < cutoffMs
|
|
247
|
+
) {
|
|
248
|
+
keysToDelete.push(key);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const key of keysToDelete) {
|
|
252
|
+
await this.duties.delete(key);
|
|
253
|
+
}
|
|
254
|
+
return keysToDelete.length;
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Close the underlying LMDB store.
|
|
260
|
+
*/
|
|
261
|
+
public async close(): Promise<void> {
|
|
262
|
+
await this.store.close();
|
|
263
|
+
this.log.debug('LMDB slashing protection database closed');
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -1,21 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Initial schema for validator HA slashing protection
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Note: this migration contains a fixed snapshot of the schema at the time it was created.
|
|
5
|
+
* It must NOT import from schema.ts, which evolves over time and would cause this migration
|
|
6
|
+
* to produce different results on fresh runs vs. re-runs.
|
|
5
7
|
*/
|
|
6
8
|
import type { MigrationBuilder } from 'node-pg-migrate';
|
|
7
9
|
|
|
8
|
-
import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE
|
|
10
|
+
import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE } from '../schema.js';
|
|
11
|
+
|
|
12
|
+
// Snapshot of the initial schema — does NOT include checkpoint_number (added in migration 2).
|
|
13
|
+
const INITIAL_SCHEMA_SETUP = [
|
|
14
|
+
`CREATE TABLE IF NOT EXISTS schema_version (
|
|
15
|
+
version INTEGER PRIMARY KEY,
|
|
16
|
+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
17
|
+
);`,
|
|
18
|
+
`CREATE TABLE IF NOT EXISTS validator_duties (
|
|
19
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
20
|
+
validator_address VARCHAR(42) NOT NULL,
|
|
21
|
+
slot BIGINT NOT NULL,
|
|
22
|
+
block_number BIGINT NOT NULL,
|
|
23
|
+
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
24
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
25
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
26
|
+
message_hash VARCHAR(66) NOT NULL,
|
|
27
|
+
signature VARCHAR(132),
|
|
28
|
+
node_id VARCHAR(255) NOT NULL,
|
|
29
|
+
lock_token VARCHAR(64) NOT NULL,
|
|
30
|
+
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
31
|
+
completed_at TIMESTAMP,
|
|
32
|
+
error_message TEXT,
|
|
33
|
+
|
|
34
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
35
|
+
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
36
|
+
);`,
|
|
37
|
+
`CREATE INDEX IF NOT EXISTS idx_validator_duties_status ON validator_duties(status, started_at);`,
|
|
38
|
+
`CREATE INDEX IF NOT EXISTS idx_validator_duties_node ON validator_duties(node_id, started_at);`,
|
|
39
|
+
] as const;
|
|
9
40
|
|
|
10
41
|
export function up(pgm: MigrationBuilder): void {
|
|
11
|
-
for (const statement of
|
|
42
|
+
for (const statement of INITIAL_SCHEMA_SETUP) {
|
|
12
43
|
pgm.sql(statement);
|
|
13
44
|
}
|
|
14
45
|
|
|
15
46
|
// Insert initial schema version
|
|
16
47
|
pgm.sql(`
|
|
17
48
|
INSERT INTO schema_version (version)
|
|
18
|
-
VALUES (
|
|
49
|
+
VALUES (1)
|
|
19
50
|
ON CONFLICT (version) DO NOTHING;
|
|
20
51
|
`);
|
|
21
52
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add checkpoint_number column to validator_duties table
|
|
3
|
+
*/
|
|
4
|
+
import type { MigrationBuilder } from 'node-pg-migrate';
|
|
5
|
+
|
|
6
|
+
export function up(pgm: MigrationBuilder): void {
|
|
7
|
+
pgm.addColumn('validator_duties', {
|
|
8
|
+
// eslint-disable-next-line camelcase
|
|
9
|
+
checkpoint_number: { type: 'bigint', notNull: true, default: 0 },
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
pgm.sql(`UPDATE schema_version SET version = 2 WHERE version = 1`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function down(pgm: MigrationBuilder): void {
|
|
16
|
+
pgm.dropColumn('validator_duties', 'checkpoint_number');
|
|
17
|
+
|
|
18
|
+
pgm.sql(`UPDATE schema_version SET version = 1 WHERE version = 2`);
|
|
19
|
+
}
|
package/src/db/postgres.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
3
|
*/
|
|
4
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
5
|
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
5
6
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
6
7
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
8
|
+
import { makeBackoff, retry } from '@aztec/foundation/retry';
|
|
7
9
|
|
|
8
|
-
import type {
|
|
10
|
+
import type { QueryResult, QueryResultRow } from 'pg';
|
|
9
11
|
|
|
10
12
|
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
11
13
|
import {
|
|
14
|
+
CLEANUP_OLD_DUTIES,
|
|
15
|
+
CLEANUP_OUTDATED_ROLLUP_DUTIES,
|
|
12
16
|
CLEANUP_OWN_STUCK_DUTIES,
|
|
13
17
|
DELETE_DUTY,
|
|
14
18
|
INSERT_OR_GET_DUTY,
|
|
@@ -16,6 +20,16 @@ import {
|
|
|
16
20
|
UPDATE_DUTY_SIGNED,
|
|
17
21
|
} from './schema.js';
|
|
18
22
|
import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, ValidatorDutyRecord } from './types.js';
|
|
23
|
+
import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Minimal pool interface for database operations.
|
|
27
|
+
* Both pg.Pool and test adapters (e.g., PGlite) satisfy this interface.
|
|
28
|
+
*/
|
|
29
|
+
export interface QueryablePool {
|
|
30
|
+
query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>>;
|
|
31
|
+
end(): Promise<void>;
|
|
32
|
+
}
|
|
19
33
|
|
|
20
34
|
/**
|
|
21
35
|
* PostgreSQL implementation of the slashing protection database
|
|
@@ -23,7 +37,7 @@ import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, Validator
|
|
|
23
37
|
export class PostgresSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
24
38
|
private readonly log: Logger;
|
|
25
39
|
|
|
26
|
-
constructor(private readonly pool:
|
|
40
|
+
constructor(private readonly pool: QueryablePool) {
|
|
27
41
|
this.log = createLogger('slashing-protection:postgres');
|
|
28
42
|
}
|
|
29
43
|
|
|
@@ -48,13 +62,13 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
48
62
|
dbVersion = result.rows[0].version;
|
|
49
63
|
} catch {
|
|
50
64
|
throw new Error(
|
|
51
|
-
'Database schema not initialized. Please run migrations first: aztec migrate up --database-url <url>',
|
|
65
|
+
'Database schema not initialized. Please run migrations first: aztec migrate-ha-db up --database-url <url>',
|
|
52
66
|
);
|
|
53
67
|
}
|
|
54
68
|
|
|
55
69
|
if (dbVersion < SCHEMA_VERSION) {
|
|
56
70
|
throw new Error(
|
|
57
|
-
`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate up --database-url <url>`,
|
|
71
|
+
`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate-ha-db up --database-url <url>`,
|
|
58
72
|
);
|
|
59
73
|
}
|
|
60
74
|
|
|
@@ -72,24 +86,56 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
72
86
|
*
|
|
73
87
|
* @returns { isNew: true, record } if we successfully inserted and acquired the lock
|
|
74
88
|
* @returns { isNew: false, record } if a record already exists. lock_token is empty if the record already exists.
|
|
89
|
+
*
|
|
90
|
+
* Retries if no rows are returned, which can happen under high concurrency
|
|
91
|
+
* when another transaction just committed the row but it's not yet visible.
|
|
75
92
|
*/
|
|
76
93
|
async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
|
|
77
94
|
// create a token for ownership verification
|
|
78
95
|
const lockToken = randomBytes(16).toString('hex');
|
|
79
96
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
97
|
+
// Use fast retries with custom backoff: 10ms, 20ms, 30ms (then stop)
|
|
98
|
+
const fastBackoff = makeBackoff([0.01, 0.02, 0.03]);
|
|
99
|
+
|
|
100
|
+
// Get the normalized block index using type-safe helper
|
|
101
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
102
|
+
|
|
103
|
+
const result = await retry<QueryResult<InsertOrGetRow>>(
|
|
104
|
+
async () => {
|
|
105
|
+
const queryResult: QueryResult<InsertOrGetRow> = await this.pool.query(INSERT_OR_GET_DUTY, [
|
|
106
|
+
params.rollupAddress.toString(),
|
|
107
|
+
params.validatorAddress.toString(),
|
|
108
|
+
params.slot.toString(),
|
|
109
|
+
params.blockNumber.toString(),
|
|
110
|
+
params.checkpointNumber.toString(),
|
|
111
|
+
blockIndexWithinCheckpoint,
|
|
112
|
+
params.dutyType,
|
|
113
|
+
params.messageHash,
|
|
114
|
+
params.nodeId,
|
|
115
|
+
lockToken,
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
// Throw error if no rows to trigger retry
|
|
119
|
+
if (queryResult.rows.length === 0) {
|
|
120
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return queryResult;
|
|
124
|
+
},
|
|
125
|
+
`INSERT_OR_GET_DUTY for node ${params.nodeId}`,
|
|
126
|
+
fastBackoff,
|
|
127
|
+
this.log,
|
|
128
|
+
true,
|
|
129
|
+
);
|
|
89
130
|
|
|
90
131
|
if (result.rows.length === 0) {
|
|
91
|
-
//
|
|
92
|
-
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
132
|
+
// this should never happen as the retry function should throw if it still fails after retries
|
|
133
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows after retries');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (result.rows.length > 1) {
|
|
137
|
+
// this should never happen if database constraints are correct (PRIMARY KEY should prevent duplicates)
|
|
138
|
+
throw new Error(`INSERT_OR_GET_DUTY returned ${result.rows.length} rows (expected exactly 1).`);
|
|
93
139
|
}
|
|
94
140
|
|
|
95
141
|
const row = result.rows[0];
|
|
@@ -106,25 +152,31 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
106
152
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
107
153
|
*/
|
|
108
154
|
async updateDutySigned(
|
|
155
|
+
rollupAddress: EthAddress,
|
|
109
156
|
validatorAddress: EthAddress,
|
|
110
|
-
slot:
|
|
157
|
+
slot: SlotNumber,
|
|
111
158
|
dutyType: DutyType,
|
|
112
159
|
signature: string,
|
|
113
160
|
lockToken: string,
|
|
161
|
+
blockIndexWithinCheckpoint: number,
|
|
114
162
|
): Promise<boolean> {
|
|
115
163
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
116
164
|
signature,
|
|
165
|
+
rollupAddress.toString(),
|
|
117
166
|
validatorAddress.toString(),
|
|
118
167
|
slot.toString(),
|
|
119
168
|
dutyType,
|
|
169
|
+
blockIndexWithinCheckpoint,
|
|
120
170
|
lockToken,
|
|
121
171
|
]);
|
|
122
172
|
|
|
123
173
|
if (result.rowCount === 0) {
|
|
124
174
|
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
175
|
+
rollupAddress: rollupAddress.toString(),
|
|
125
176
|
validatorAddress: validatorAddress.toString(),
|
|
126
177
|
slot: slot.toString(),
|
|
127
178
|
dutyType,
|
|
179
|
+
blockIndexWithinCheckpoint,
|
|
128
180
|
});
|
|
129
181
|
return false;
|
|
130
182
|
}
|
|
@@ -139,23 +191,29 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
139
191
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
140
192
|
*/
|
|
141
193
|
async deleteDuty(
|
|
194
|
+
rollupAddress: EthAddress,
|
|
142
195
|
validatorAddress: EthAddress,
|
|
143
|
-
slot:
|
|
196
|
+
slot: SlotNumber,
|
|
144
197
|
dutyType: DutyType,
|
|
145
198
|
lockToken: string,
|
|
199
|
+
blockIndexWithinCheckpoint: number,
|
|
146
200
|
): Promise<boolean> {
|
|
147
201
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
202
|
+
rollupAddress.toString(),
|
|
148
203
|
validatorAddress.toString(),
|
|
149
204
|
slot.toString(),
|
|
150
205
|
dutyType,
|
|
206
|
+
blockIndexWithinCheckpoint,
|
|
151
207
|
lockToken,
|
|
152
208
|
]);
|
|
153
209
|
|
|
154
210
|
if (result.rowCount === 0) {
|
|
155
211
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
212
|
+
rollupAddress: rollupAddress.toString(),
|
|
156
213
|
validatorAddress: validatorAddress.toString(),
|
|
157
214
|
slot: slot.toString(),
|
|
158
215
|
dutyType,
|
|
216
|
+
blockIndexWithinCheckpoint,
|
|
159
217
|
});
|
|
160
218
|
return false;
|
|
161
219
|
}
|
|
@@ -163,23 +221,28 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
163
221
|
}
|
|
164
222
|
|
|
165
223
|
/**
|
|
166
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
224
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
225
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
226
|
+
* then delegates to the shared recordFromFields() converter.
|
|
167
227
|
*/
|
|
168
228
|
private rowToRecord(row: DutyRow): ValidatorDutyRecord {
|
|
169
|
-
return {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
229
|
+
return recordFromFields({
|
|
230
|
+
rollupAddress: row.rollup_address,
|
|
231
|
+
validatorAddress: row.validator_address,
|
|
232
|
+
slot: row.slot,
|
|
233
|
+
blockNumber: row.block_number,
|
|
234
|
+
checkpointNumber: row.checkpoint_number,
|
|
235
|
+
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
173
236
|
dutyType: row.duty_type,
|
|
174
237
|
status: row.status,
|
|
175
238
|
messageHash: row.message_hash,
|
|
176
239
|
signature: row.signature ?? undefined,
|
|
177
240
|
nodeId: row.node_id,
|
|
178
241
|
lockToken: row.lock_token,
|
|
179
|
-
|
|
180
|
-
|
|
242
|
+
startedAtMs: row.started_at.getTime(),
|
|
243
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
181
244
|
errorMessage: row.error_message ?? undefined,
|
|
182
|
-
};
|
|
245
|
+
});
|
|
183
246
|
}
|
|
184
247
|
|
|
185
248
|
/**
|
|
@@ -195,8 +258,29 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
195
258
|
* @returns the number of duties cleaned up
|
|
196
259
|
*/
|
|
197
260
|
async cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
198
|
-
const
|
|
199
|
-
|
|
261
|
+
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, maxAgeMs]);
|
|
262
|
+
return result.rowCount ?? 0;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Cleanup duties with outdated rollup address.
|
|
267
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
268
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
269
|
+
* @returns the number of duties cleaned up
|
|
270
|
+
*/
|
|
271
|
+
async cleanupOutdatedRollupDuties(currentRollupAddress: EthAddress): Promise<number> {
|
|
272
|
+
const result = await this.pool.query(CLEANUP_OUTDATED_ROLLUP_DUTIES, [currentRollupAddress.toString()]);
|
|
273
|
+
return result.rowCount ?? 0;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Cleanup old signed duties.
|
|
278
|
+
* Removes only signed duties older than the specified age.
|
|
279
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
280
|
+
* @returns the number of duties cleaned up
|
|
281
|
+
*/
|
|
282
|
+
async cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
283
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [maxAgeMs]);
|
|
200
284
|
return result.rowCount ?? 0;
|
|
201
285
|
}
|
|
202
286
|
}
|