@aztec/validator-ha-signer 0.0.1-commit.d1f2d6c → 0.0.1-commit.d20b825a7
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 +10 -2
- 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 +20 -4
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +46 -17
- package/dest/db/schema.d.ts +18 -11
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +45 -23
- package/dest/db/types.d.ts +52 -22
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +31 -15
- package/dest/factory.d.ts +39 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +78 -7
- package/dest/metrics.d.ts +51 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +103 -0
- package/dest/slashing_protection_service.d.ts +19 -6
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +57 -17
- package/dest/types.d.ts +33 -72
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -20
- package/dest/validator_ha_signer.d.ts +15 -6
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +26 -11
- package/package.json +10 -5
- 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 +47 -12
- package/src/db/schema.ts +47 -23
- package/src/db/types.ts +72 -20
- package/src/factory.ts +96 -6
- package/src/metrics.ts +138 -0
- package/src/slashing_protection_service.ts +79 -21
- package/src/types.ts +56 -103
- package/src/validator_ha_signer.ts +44 -15
- package/dest/config.d.ts +0 -79
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -73
- package/src/config.ts +0 -125
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,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
5
5
|
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
6
6
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
7
7
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
@@ -11,6 +11,8 @@ import type { QueryResult, QueryResultRow } from 'pg';
|
|
|
11
11
|
|
|
12
12
|
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
13
13
|
import {
|
|
14
|
+
CLEANUP_OLD_DUTIES,
|
|
15
|
+
CLEANUP_OUTDATED_ROLLUP_DUTIES,
|
|
14
16
|
CLEANUP_OWN_STUCK_DUTIES,
|
|
15
17
|
DELETE_DUTY,
|
|
16
18
|
INSERT_OR_GET_DUTY,
|
|
@@ -18,7 +20,7 @@ import {
|
|
|
18
20
|
UPDATE_DUTY_SIGNED,
|
|
19
21
|
} from './schema.js';
|
|
20
22
|
import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, ValidatorDutyRecord } from './types.js';
|
|
21
|
-
import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
23
|
+
import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* Minimal pool interface for database operations.
|
|
@@ -101,9 +103,11 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
101
103
|
const result = await retry<QueryResult<InsertOrGetRow>>(
|
|
102
104
|
async () => {
|
|
103
105
|
const queryResult: QueryResult<InsertOrGetRow> = await this.pool.query(INSERT_OR_GET_DUTY, [
|
|
106
|
+
params.rollupAddress.toString(),
|
|
104
107
|
params.validatorAddress.toString(),
|
|
105
108
|
params.slot.toString(),
|
|
106
109
|
params.blockNumber.toString(),
|
|
110
|
+
params.checkpointNumber.toString(),
|
|
107
111
|
blockIndexWithinCheckpoint,
|
|
108
112
|
params.dutyType,
|
|
109
113
|
params.messageHash,
|
|
@@ -148,6 +152,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
148
152
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
149
153
|
*/
|
|
150
154
|
async updateDutySigned(
|
|
155
|
+
rollupAddress: EthAddress,
|
|
151
156
|
validatorAddress: EthAddress,
|
|
152
157
|
slot: SlotNumber,
|
|
153
158
|
dutyType: DutyType,
|
|
@@ -157,6 +162,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
157
162
|
): Promise<boolean> {
|
|
158
163
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
159
164
|
signature,
|
|
165
|
+
rollupAddress.toString(),
|
|
160
166
|
validatorAddress.toString(),
|
|
161
167
|
slot.toString(),
|
|
162
168
|
dutyType,
|
|
@@ -166,6 +172,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
166
172
|
|
|
167
173
|
if (result.rowCount === 0) {
|
|
168
174
|
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
175
|
+
rollupAddress: rollupAddress.toString(),
|
|
169
176
|
validatorAddress: validatorAddress.toString(),
|
|
170
177
|
slot: slot.toString(),
|
|
171
178
|
dutyType,
|
|
@@ -184,6 +191,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
184
191
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
185
192
|
*/
|
|
186
193
|
async deleteDuty(
|
|
194
|
+
rollupAddress: EthAddress,
|
|
187
195
|
validatorAddress: EthAddress,
|
|
188
196
|
slot: SlotNumber,
|
|
189
197
|
dutyType: DutyType,
|
|
@@ -191,6 +199,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
191
199
|
blockIndexWithinCheckpoint: number,
|
|
192
200
|
): Promise<boolean> {
|
|
193
201
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
202
|
+
rollupAddress.toString(),
|
|
194
203
|
validatorAddress.toString(),
|
|
195
204
|
slot.toString(),
|
|
196
205
|
dutyType,
|
|
@@ -200,6 +209,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
200
209
|
|
|
201
210
|
if (result.rowCount === 0) {
|
|
202
211
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
212
|
+
rollupAddress: rollupAddress.toString(),
|
|
203
213
|
validatorAddress: validatorAddress.toString(),
|
|
204
214
|
slot: slot.toString(),
|
|
205
215
|
dutyType,
|
|
@@ -211,13 +221,17 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
211
221
|
}
|
|
212
222
|
|
|
213
223
|
/**
|
|
214
|
-
* 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.
|
|
215
227
|
*/
|
|
216
228
|
private rowToRecord(row: DutyRow): ValidatorDutyRecord {
|
|
217
|
-
return {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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,
|
|
221
235
|
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
222
236
|
dutyType: row.duty_type,
|
|
223
237
|
status: row.status,
|
|
@@ -225,10 +239,10 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
225
239
|
signature: row.signature ?? undefined,
|
|
226
240
|
nodeId: row.node_id,
|
|
227
241
|
lockToken: row.lock_token,
|
|
228
|
-
|
|
229
|
-
|
|
242
|
+
startedAtMs: row.started_at.getTime(),
|
|
243
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
230
244
|
errorMessage: row.error_message ?? undefined,
|
|
231
|
-
};
|
|
245
|
+
});
|
|
232
246
|
}
|
|
233
247
|
|
|
234
248
|
/**
|
|
@@ -244,8 +258,29 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
244
258
|
* @returns the number of duties cleaned up
|
|
245
259
|
*/
|
|
246
260
|
async cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
247
|
-
const
|
|
248
|
-
|
|
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]);
|
|
249
284
|
return result.rowCount ?? 0;
|
|
250
285
|
}
|
|
251
286
|
}
|
package/src/db/schema.ts
CHANGED
|
@@ -9,19 +9,21 @@
|
|
|
9
9
|
/**
|
|
10
10
|
* Current schema version
|
|
11
11
|
*/
|
|
12
|
-
export const SCHEMA_VERSION =
|
|
12
|
+
export const SCHEMA_VERSION = 2;
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* SQL to create the validator_duties table
|
|
16
16
|
*/
|
|
17
17
|
export const CREATE_VALIDATOR_DUTIES_TABLE = `
|
|
18
18
|
CREATE TABLE IF NOT EXISTS validator_duties (
|
|
19
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
19
20
|
validator_address VARCHAR(42) NOT NULL,
|
|
20
21
|
slot BIGINT NOT NULL,
|
|
21
22
|
block_number BIGINT NOT NULL,
|
|
23
|
+
checkpoint_number BIGINT NOT NULL DEFAULT 0,
|
|
22
24
|
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
23
25
|
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
24
|
-
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed'
|
|
26
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
25
27
|
message_hash VARCHAR(66) NOT NULL,
|
|
26
28
|
signature VARCHAR(132),
|
|
27
29
|
node_id VARCHAR(255) NOT NULL,
|
|
@@ -30,7 +32,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
|
|
|
30
32
|
completed_at TIMESTAMP,
|
|
31
33
|
error_message TEXT,
|
|
32
34
|
|
|
33
|
-
PRIMARY KEY (validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
35
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
34
36
|
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
35
37
|
);
|
|
36
38
|
`;
|
|
@@ -101,9 +103,11 @@ SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
|
101
103
|
export const INSERT_OR_GET_DUTY = `
|
|
102
104
|
WITH inserted AS (
|
|
103
105
|
INSERT INTO validator_duties (
|
|
106
|
+
rollup_address,
|
|
104
107
|
validator_address,
|
|
105
108
|
slot,
|
|
106
109
|
block_number,
|
|
110
|
+
checkpoint_number,
|
|
107
111
|
block_index_within_checkpoint,
|
|
108
112
|
duty_type,
|
|
109
113
|
status,
|
|
@@ -111,12 +115,14 @@ WITH inserted AS (
|
|
|
111
115
|
node_id,
|
|
112
116
|
lock_token,
|
|
113
117
|
started_at
|
|
114
|
-
) VALUES ($1, $2, $3, $4, $5, 'signing', $
|
|
115
|
-
ON CONFLICT (validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
118
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, 'signing', $8, $9, $10, CURRENT_TIMESTAMP)
|
|
119
|
+
ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
116
120
|
RETURNING
|
|
121
|
+
rollup_address,
|
|
117
122
|
validator_address,
|
|
118
123
|
slot,
|
|
119
124
|
block_number,
|
|
125
|
+
checkpoint_number,
|
|
120
126
|
block_index_within_checkpoint,
|
|
121
127
|
duty_type,
|
|
122
128
|
status,
|
|
@@ -132,9 +138,11 @@ WITH inserted AS (
|
|
|
132
138
|
SELECT * FROM inserted
|
|
133
139
|
UNION ALL
|
|
134
140
|
SELECT
|
|
141
|
+
rollup_address,
|
|
135
142
|
validator_address,
|
|
136
143
|
slot,
|
|
137
144
|
block_number,
|
|
145
|
+
checkpoint_number,
|
|
138
146
|
block_index_within_checkpoint,
|
|
139
147
|
duty_type,
|
|
140
148
|
status,
|
|
@@ -147,10 +155,11 @@ SELECT
|
|
|
147
155
|
error_message,
|
|
148
156
|
FALSE as is_new
|
|
149
157
|
FROM validator_duties
|
|
150
|
-
WHERE
|
|
151
|
-
AND
|
|
152
|
-
AND
|
|
153
|
-
AND
|
|
158
|
+
WHERE rollup_address = $1
|
|
159
|
+
AND validator_address = $2
|
|
160
|
+
AND slot = $3
|
|
161
|
+
AND duty_type = $7
|
|
162
|
+
AND block_index_within_checkpoint = $6
|
|
154
163
|
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
155
164
|
`;
|
|
156
165
|
|
|
@@ -162,12 +171,13 @@ UPDATE validator_duties
|
|
|
162
171
|
SET status = 'signed',
|
|
163
172
|
signature = $1,
|
|
164
173
|
completed_at = CURRENT_TIMESTAMP
|
|
165
|
-
WHERE
|
|
166
|
-
AND
|
|
167
|
-
AND
|
|
168
|
-
AND
|
|
174
|
+
WHERE rollup_address = $2
|
|
175
|
+
AND validator_address = $3
|
|
176
|
+
AND slot = $4
|
|
177
|
+
AND duty_type = $5
|
|
178
|
+
AND block_index_within_checkpoint = $6
|
|
169
179
|
AND status = 'signing'
|
|
170
|
-
AND lock_token = $
|
|
180
|
+
AND lock_token = $7;
|
|
171
181
|
`;
|
|
172
182
|
|
|
173
183
|
/**
|
|
@@ -176,12 +186,13 @@ WHERE validator_address = $2
|
|
|
176
186
|
*/
|
|
177
187
|
export const DELETE_DUTY = `
|
|
178
188
|
DELETE FROM validator_duties
|
|
179
|
-
WHERE
|
|
180
|
-
AND
|
|
181
|
-
AND
|
|
182
|
-
AND
|
|
189
|
+
WHERE rollup_address = $1
|
|
190
|
+
AND validator_address = $2
|
|
191
|
+
AND slot = $3
|
|
192
|
+
AND duty_type = $4
|
|
193
|
+
AND block_index_within_checkpoint = $5
|
|
183
194
|
AND status = 'signing'
|
|
184
|
-
AND lock_token = $
|
|
195
|
+
AND lock_token = $6;
|
|
185
196
|
`;
|
|
186
197
|
|
|
187
198
|
/**
|
|
@@ -196,23 +207,34 @@ WHERE status = 'signed'
|
|
|
196
207
|
|
|
197
208
|
/**
|
|
198
209
|
* Query to clean up old duties (for maintenance)
|
|
199
|
-
* Removes duties older than a specified
|
|
210
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
200
211
|
*/
|
|
201
212
|
export const CLEANUP_OLD_DUTIES = `
|
|
202
213
|
DELETE FROM validator_duties
|
|
203
|
-
WHERE status
|
|
204
|
-
AND started_at < $1;
|
|
214
|
+
WHERE status = 'signed'
|
|
215
|
+
AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
|
|
205
216
|
`;
|
|
206
217
|
|
|
207
218
|
/**
|
|
208
219
|
* Query to cleanup own stuck duties
|
|
209
220
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
221
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
210
222
|
*/
|
|
211
223
|
export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
212
224
|
DELETE FROM validator_duties
|
|
213
225
|
WHERE node_id = $1
|
|
214
226
|
AND status = 'signing'
|
|
215
|
-
AND started_at < $2;
|
|
227
|
+
AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
|
|
228
|
+
`;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Query to cleanup duties with outdated rollup address
|
|
232
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
233
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
234
|
+
*/
|
|
235
|
+
export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
|
|
236
|
+
DELETE FROM validator_duties
|
|
237
|
+
WHERE rollup_address != $1;
|
|
216
238
|
`;
|
|
217
239
|
|
|
218
240
|
/**
|
|
@@ -231,9 +253,11 @@ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
|
|
|
231
253
|
*/
|
|
232
254
|
export const GET_STUCK_DUTIES = `
|
|
233
255
|
SELECT
|
|
256
|
+
rollup_address,
|
|
234
257
|
validator_address,
|
|
235
258
|
slot,
|
|
236
259
|
block_number,
|
|
260
|
+
checkpoint_number,
|
|
237
261
|
block_index_within_checkpoint,
|
|
238
262
|
duty_type,
|
|
239
263
|
status,
|