@aztec/validator-ha-signer 0.0.1-commit.733c4a3 → 0.0.1-commit.7689c7f
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 +0 -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 +70 -0
- package/dest/db/lmdb.d.ts.map +1 -0
- package/dest/db/lmdb.js +223 -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 +4 -2
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +15 -13
- package/dest/db/schema.d.ts +6 -6
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +9 -4
- package/dest/db/types.d.ts +44 -7
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +26 -0
- package/dest/factory.d.ts +39 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +71 -7
- package/dest/slashing_protection_service.d.ts +3 -3
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +4 -4
- package/dest/types.d.ts +8 -3
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +2 -1
- package/dest/validator_ha_signer.d.ts +3 -3
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +4 -6
- package/package.json +9 -7
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +308 -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 +15 -11
- package/src/db/schema.ts +9 -4
- package/src/db/types.ts +63 -6
- package/src/factory.ts +100 -6
- package/src/slashing_protection_service.ts +6 -6
- package/src/types.ts +11 -0
- package/src/validator_ha_signer.ts +6 -8
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* This ensures that even with multiple validator nodes running, only one
|
|
6
6
|
* node will sign for a given duty (slot + duty type).
|
|
7
7
|
*/ import { createLogger } from '@aztec/foundation/log';
|
|
8
|
-
import { DutyType, getBlockNumberFromSigningContext } from '@aztec/stdlib/ha-signing';
|
|
8
|
+
import { DutyType, getBlockNumberFromSigningContext, getCheckpointNumberFromSigningContext } from '@aztec/stdlib/ha-signing';
|
|
9
9
|
import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
10
10
|
/**
|
|
11
11
|
* Validator High Availability Signer
|
|
@@ -37,14 +37,10 @@ import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
|
37
37
|
this.log = createLogger('validator-ha-signer');
|
|
38
38
|
this.metrics = deps.metrics;
|
|
39
39
|
this.dateProvider = deps.dateProvider;
|
|
40
|
-
if (!config.haSigningEnabled) {
|
|
41
|
-
// this shouldn't happen, the validator should use different signer for non-HA setups
|
|
42
|
-
throw new Error('Validator HA Signer is not enabled in config');
|
|
43
|
-
}
|
|
44
40
|
if (!config.nodeId || config.nodeId === '') {
|
|
45
41
|
throw new Error('NODE_ID is required for high-availability setups');
|
|
46
42
|
}
|
|
47
|
-
this.rollupAddress = config.
|
|
43
|
+
this.rollupAddress = config.rollupAddress;
|
|
48
44
|
this.slashingProtection = new SlashingProtectionService(db, config, {
|
|
49
45
|
metrics: deps.metrics,
|
|
50
46
|
dateProvider: deps.dateProvider
|
|
@@ -93,9 +89,11 @@ import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
|
93
89
|
// Acquire lock and get the token for ownership verification
|
|
94
90
|
// DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
|
|
95
91
|
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
92
|
+
const checkpointNumber = getCheckpointNumberFromSigningContext(context);
|
|
96
93
|
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
97
94
|
...dutyIdentifier,
|
|
98
95
|
blockNumber,
|
|
96
|
+
checkpointNumber,
|
|
99
97
|
messageHash: messageHash.toString(),
|
|
100
98
|
nodeId: this.config.nodeId
|
|
101
99
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-ha-signer",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.7689c7f",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./db": "./dest/db/index.js",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"./slashing-protection-service": "./dest/slashing_protection_service.js",
|
|
12
12
|
"./types": "./dest/types.js",
|
|
13
13
|
"./validator-ha-signer": "./dest/validator_ha_signer.js",
|
|
14
|
-
"./test": "./dest/test/pglite_pool.js"
|
|
14
|
+
"./test": "./dest/test/pglite_pool.js",
|
|
15
|
+
"./db/lmdb": "./dest/db/lmdb.js"
|
|
15
16
|
},
|
|
16
17
|
"typedocOptions": {
|
|
17
18
|
"entryPoints": [
|
|
@@ -74,14 +75,15 @@
|
|
|
74
75
|
]
|
|
75
76
|
},
|
|
76
77
|
"dependencies": {
|
|
77
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
78
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
79
|
-
"@aztec/
|
|
80
|
-
"@aztec/
|
|
78
|
+
"@aztec/ethereum": "0.0.1-commit.7689c7f",
|
|
79
|
+
"@aztec/foundation": "0.0.1-commit.7689c7f",
|
|
80
|
+
"@aztec/kv-store": "0.0.1-commit.7689c7f",
|
|
81
|
+
"@aztec/stdlib": "0.0.1-commit.7689c7f",
|
|
82
|
+
"@aztec/telemetry-client": "0.0.1-commit.7689c7f",
|
|
81
83
|
"node-pg-migrate": "^8.0.4",
|
|
82
84
|
"pg": "^8.11.3",
|
|
83
85
|
"tslib": "^2.4.0",
|
|
84
|
-
"zod": "^
|
|
86
|
+
"zod": "^4"
|
|
85
87
|
},
|
|
86
88
|
"devDependencies": {
|
|
87
89
|
"@electric-sql/pglite": "^0.3.14",
|
package/src/db/index.ts
CHANGED
package/src/db/lmdb.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
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
|
+
import { openStoreAt } from '@aztec/kv-store/lmdb-v2';
|
|
17
|
+
|
|
18
|
+
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
19
|
+
import {
|
|
20
|
+
type CheckAndRecordParams,
|
|
21
|
+
DutyStatus,
|
|
22
|
+
DutyType,
|
|
23
|
+
type StoredDutyRecord,
|
|
24
|
+
getBlockIndexFromDutyIdentifier,
|
|
25
|
+
recordFromFields,
|
|
26
|
+
} from './types.js';
|
|
27
|
+
|
|
28
|
+
const DUTIES_MAP_NAME = 'signing-protection-duties';
|
|
29
|
+
const LEGACY_CHECKPOINT_NUMBER = '0';
|
|
30
|
+
|
|
31
|
+
type StoredDutyRecordV1 = Omit<StoredDutyRecord, 'checkpointNumber'> & { checkpointNumber?: undefined };
|
|
32
|
+
type MigratableStoredDutyRecord = StoredDutyRecord | StoredDutyRecordV1;
|
|
33
|
+
|
|
34
|
+
function needsCheckpointNumberMigration(record: MigratableStoredDutyRecord): record is StoredDutyRecordV1 {
|
|
35
|
+
return record.checkpointNumber === undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Migrates local slashing-protection duties from schema 1 to schema 2.
|
|
40
|
+
*/
|
|
41
|
+
export async function migrateLmdbSlashingProtectionDatabase(
|
|
42
|
+
dataDirectory: string,
|
|
43
|
+
currentVersion: number,
|
|
44
|
+
latestVersion: number,
|
|
45
|
+
dbMapSizeKb?: number,
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
if (currentVersion !== 1 || latestVersion !== LmdbSlashingProtectionDatabase.SCHEMA_VERSION) {
|
|
48
|
+
throw new Error(`Unsupported LMDB slashing-protection migration ${currentVersion} -> ${latestVersion}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const store = await openStoreAt(dataDirectory, dbMapSizeKb);
|
|
52
|
+
try {
|
|
53
|
+
const duties = store.openMap<string, MigratableStoredDutyRecord>(DUTIES_MAP_NAME);
|
|
54
|
+
const migratedRecords: { key: string; value: StoredDutyRecord }[] = [];
|
|
55
|
+
|
|
56
|
+
for await (const [key, record] of duties.entriesAsync()) {
|
|
57
|
+
if (needsCheckpointNumberMigration(record)) {
|
|
58
|
+
migratedRecords.push({ key, value: { ...record, checkpointNumber: LEGACY_CHECKPOINT_NUMBER } });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (migratedRecords.length > 0) {
|
|
63
|
+
await duties.setMany(migratedRecords);
|
|
64
|
+
}
|
|
65
|
+
} finally {
|
|
66
|
+
await store.close();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function dutyKey(
|
|
71
|
+
rollupAddress: string,
|
|
72
|
+
validatorAddress: string,
|
|
73
|
+
slot: string,
|
|
74
|
+
dutyType: string,
|
|
75
|
+
blockIndexWithinCheckpoint: number,
|
|
76
|
+
): string {
|
|
77
|
+
return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* LMDB-backed implementation of SlashingProtectionDatabase.
|
|
82
|
+
*
|
|
83
|
+
* Provides single-node double-signing protection that survives crashes and restarts.
|
|
84
|
+
* Does not provide cross-node coordination (that requires the PostgreSQL implementation).
|
|
85
|
+
*/
|
|
86
|
+
export class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
87
|
+
public static readonly SCHEMA_VERSION = 2;
|
|
88
|
+
|
|
89
|
+
private readonly duties: AztecAsyncMap<string, StoredDutyRecord>;
|
|
90
|
+
private readonly log: Logger;
|
|
91
|
+
|
|
92
|
+
constructor(
|
|
93
|
+
private readonly store: AztecAsyncKVStore,
|
|
94
|
+
private readonly dateProvider: DateProvider,
|
|
95
|
+
) {
|
|
96
|
+
this.log = createLogger('slashing-protection:lmdb');
|
|
97
|
+
this.duties = store.openMap<string, StoredDutyRecord>(DUTIES_MAP_NAME);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
102
|
+
*
|
|
103
|
+
* LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
|
|
104
|
+
*/
|
|
105
|
+
public async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
|
|
106
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
107
|
+
const key = dutyKey(
|
|
108
|
+
params.rollupAddress.toString(),
|
|
109
|
+
params.validatorAddress.toString(),
|
|
110
|
+
params.slot.toString(),
|
|
111
|
+
params.dutyType,
|
|
112
|
+
blockIndexWithinCheckpoint,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const lockToken = randomBytes(16).toString('hex');
|
|
116
|
+
const now = this.dateProvider.now();
|
|
117
|
+
|
|
118
|
+
const result = await this.store.transactionAsync(async () => {
|
|
119
|
+
const existing = await this.duties.getAsync(key);
|
|
120
|
+
if (existing) {
|
|
121
|
+
return { isNew: false as const, record: { ...existing, lockToken: '' } };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const newRecord: StoredDutyRecord = {
|
|
125
|
+
rollupAddress: params.rollupAddress.toString(),
|
|
126
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
127
|
+
slot: params.slot.toString(),
|
|
128
|
+
blockNumber: params.blockNumber.toString(),
|
|
129
|
+
checkpointNumber: params.checkpointNumber.toString(),
|
|
130
|
+
blockIndexWithinCheckpoint,
|
|
131
|
+
dutyType: params.dutyType,
|
|
132
|
+
status: DutyStatus.SIGNING,
|
|
133
|
+
messageHash: params.messageHash,
|
|
134
|
+
nodeId: params.nodeId,
|
|
135
|
+
lockToken,
|
|
136
|
+
startedAtMs: now,
|
|
137
|
+
};
|
|
138
|
+
await this.duties.set(key, newRecord);
|
|
139
|
+
return { isNew: true as const, record: newRecord };
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
if (result.isNew) {
|
|
143
|
+
this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
|
|
144
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
145
|
+
nodeId: params.nodeId,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { isNew: result.isNew, record: recordFromFields(result.record) };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Update a duty to 'signed' status with the signature.
|
|
154
|
+
* Only succeeds if the lockToken matches.
|
|
155
|
+
*/
|
|
156
|
+
public updateDutySigned(
|
|
157
|
+
rollupAddress: EthAddress,
|
|
158
|
+
validatorAddress: EthAddress,
|
|
159
|
+
slot: SlotNumber,
|
|
160
|
+
dutyType: DutyType,
|
|
161
|
+
signature: string,
|
|
162
|
+
lockToken: string,
|
|
163
|
+
blockIndexWithinCheckpoint: number,
|
|
164
|
+
): Promise<boolean> {
|
|
165
|
+
const key = dutyKey(
|
|
166
|
+
rollupAddress.toString(),
|
|
167
|
+
validatorAddress.toString(),
|
|
168
|
+
slot.toString(),
|
|
169
|
+
dutyType,
|
|
170
|
+
blockIndexWithinCheckpoint,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
return this.store.transactionAsync(async () => {
|
|
174
|
+
const existing = await this.duties.getAsync(key);
|
|
175
|
+
if (!existing) {
|
|
176
|
+
this.log.warn('Failed to update duty to signed: duty not found', {
|
|
177
|
+
rollupAddress: rollupAddress.toString(),
|
|
178
|
+
validatorAddress: validatorAddress.toString(),
|
|
179
|
+
slot: slot.toString(),
|
|
180
|
+
dutyType,
|
|
181
|
+
blockIndexWithinCheckpoint,
|
|
182
|
+
});
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (existing.lockToken !== lockToken) {
|
|
187
|
+
this.log.warn('Failed to update duty to signed: invalid token', {
|
|
188
|
+
rollupAddress: rollupAddress.toString(),
|
|
189
|
+
validatorAddress: validatorAddress.toString(),
|
|
190
|
+
slot: slot.toString(),
|
|
191
|
+
dutyType,
|
|
192
|
+
blockIndexWithinCheckpoint,
|
|
193
|
+
});
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
await this.duties.set(key, {
|
|
198
|
+
...existing,
|
|
199
|
+
status: DutyStatus.SIGNED,
|
|
200
|
+
signature,
|
|
201
|
+
completedAtMs: this.dateProvider.now(),
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return true;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Delete a duty record.
|
|
210
|
+
* Only succeeds if the lockToken matches.
|
|
211
|
+
*/
|
|
212
|
+
public deleteDuty(
|
|
213
|
+
rollupAddress: EthAddress,
|
|
214
|
+
validatorAddress: EthAddress,
|
|
215
|
+
slot: SlotNumber,
|
|
216
|
+
dutyType: DutyType,
|
|
217
|
+
lockToken: string,
|
|
218
|
+
blockIndexWithinCheckpoint: number,
|
|
219
|
+
): Promise<boolean> {
|
|
220
|
+
const key = dutyKey(
|
|
221
|
+
rollupAddress.toString(),
|
|
222
|
+
validatorAddress.toString(),
|
|
223
|
+
slot.toString(),
|
|
224
|
+
dutyType,
|
|
225
|
+
blockIndexWithinCheckpoint,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
return this.store.transactionAsync(async () => {
|
|
229
|
+
const existing = await this.duties.getAsync(key);
|
|
230
|
+
if (!existing || existing.lockToken !== lockToken) {
|
|
231
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
232
|
+
rollupAddress: rollupAddress.toString(),
|
|
233
|
+
validatorAddress: validatorAddress.toString(),
|
|
234
|
+
slot: slot.toString(),
|
|
235
|
+
dutyType,
|
|
236
|
+
blockIndexWithinCheckpoint,
|
|
237
|
+
});
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
await this.duties.delete(key);
|
|
242
|
+
return true;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
248
|
+
*/
|
|
249
|
+
public cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
250
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
251
|
+
|
|
252
|
+
return this.store.transactionAsync(async () => {
|
|
253
|
+
const keysToDelete: string[] = [];
|
|
254
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
255
|
+
if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
|
|
256
|
+
keysToDelete.push(key);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const key of keysToDelete) {
|
|
260
|
+
await this.duties.delete(key);
|
|
261
|
+
}
|
|
262
|
+
return keysToDelete.length;
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Cleanup duties with outdated rollup address.
|
|
268
|
+
*
|
|
269
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
270
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
271
|
+
* startup whenever the rollup address changes.
|
|
272
|
+
*/
|
|
273
|
+
public cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
|
|
274
|
+
return Promise.resolve(0);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
279
|
+
*/
|
|
280
|
+
public cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
281
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
282
|
+
|
|
283
|
+
return this.store.transactionAsync(async () => {
|
|
284
|
+
const keysToDelete: string[] = [];
|
|
285
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
286
|
+
if (
|
|
287
|
+
record.status === DutyStatus.SIGNED &&
|
|
288
|
+
record.completedAtMs !== undefined &&
|
|
289
|
+
record.completedAtMs < cutoffMs
|
|
290
|
+
) {
|
|
291
|
+
keysToDelete.push(key);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (const key of keysToDelete) {
|
|
295
|
+
await this.duties.delete(key);
|
|
296
|
+
}
|
|
297
|
+
return keysToDelete.length;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Close the underlying LMDB store.
|
|
303
|
+
*/
|
|
304
|
+
public async close(): Promise<void> {
|
|
305
|
+
await this.store.close();
|
|
306
|
+
this.log.debug('LMDB slashing protection database closed');
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -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';
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
UPDATE_DUTY_SIGNED,
|
|
21
21
|
} from './schema.js';
|
|
22
22
|
import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, ValidatorDutyRecord } from './types.js';
|
|
23
|
-
import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
23
|
+
import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Minimal pool interface for database operations.
|
|
@@ -107,6 +107,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
107
107
|
params.validatorAddress.toString(),
|
|
108
108
|
params.slot.toString(),
|
|
109
109
|
params.blockNumber.toString(),
|
|
110
|
+
params.checkpointNumber.toString(),
|
|
110
111
|
blockIndexWithinCheckpoint,
|
|
111
112
|
params.dutyType,
|
|
112
113
|
params.messageHash,
|
|
@@ -220,14 +221,17 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
220
221
|
}
|
|
221
222
|
|
|
222
223
|
/**
|
|
223
|
-
* 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.
|
|
224
227
|
*/
|
|
225
228
|
private rowToRecord(row: DutyRow): ValidatorDutyRecord {
|
|
226
|
-
return {
|
|
227
|
-
rollupAddress:
|
|
228
|
-
validatorAddress:
|
|
229
|
-
slot:
|
|
230
|
-
blockNumber:
|
|
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,
|
|
231
235
|
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
232
236
|
dutyType: row.duty_type,
|
|
233
237
|
status: row.status,
|
|
@@ -235,10 +239,10 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
235
239
|
signature: row.signature ?? undefined,
|
|
236
240
|
nodeId: row.node_id,
|
|
237
241
|
lockToken: row.lock_token,
|
|
238
|
-
|
|
239
|
-
|
|
242
|
+
startedAtMs: row.started_at.getTime(),
|
|
243
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
240
244
|
errorMessage: row.error_message ?? undefined,
|
|
241
|
-
};
|
|
245
|
+
});
|
|
242
246
|
}
|
|
243
247
|
|
|
244
248
|
/**
|
package/src/db/schema.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
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
|
|
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
|
|
|
20
20
|
validator_address VARCHAR(42) NOT NULL,
|
|
21
21
|
slot BIGINT NOT NULL,
|
|
22
22
|
block_number BIGINT NOT NULL,
|
|
23
|
+
checkpoint_number BIGINT NOT NULL DEFAULT 0,
|
|
23
24
|
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
24
25
|
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
25
26
|
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
@@ -106,6 +107,7 @@ WITH inserted AS (
|
|
|
106
107
|
validator_address,
|
|
107
108
|
slot,
|
|
108
109
|
block_number,
|
|
110
|
+
checkpoint_number,
|
|
109
111
|
block_index_within_checkpoint,
|
|
110
112
|
duty_type,
|
|
111
113
|
status,
|
|
@@ -113,13 +115,14 @@ WITH inserted AS (
|
|
|
113
115
|
node_id,
|
|
114
116
|
lock_token,
|
|
115
117
|
started_at
|
|
116
|
-
) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $
|
|
118
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, 'signing', $8, $9, $10, CURRENT_TIMESTAMP)
|
|
117
119
|
ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
118
120
|
RETURNING
|
|
119
121
|
rollup_address,
|
|
120
122
|
validator_address,
|
|
121
123
|
slot,
|
|
122
124
|
block_number,
|
|
125
|
+
checkpoint_number,
|
|
123
126
|
block_index_within_checkpoint,
|
|
124
127
|
duty_type,
|
|
125
128
|
status,
|
|
@@ -139,6 +142,7 @@ SELECT
|
|
|
139
142
|
validator_address,
|
|
140
143
|
slot,
|
|
141
144
|
block_number,
|
|
145
|
+
checkpoint_number,
|
|
142
146
|
block_index_within_checkpoint,
|
|
143
147
|
duty_type,
|
|
144
148
|
status,
|
|
@@ -154,8 +158,8 @@ FROM validator_duties
|
|
|
154
158
|
WHERE rollup_address = $1
|
|
155
159
|
AND validator_address = $2
|
|
156
160
|
AND slot = $3
|
|
157
|
-
AND duty_type = $
|
|
158
|
-
AND block_index_within_checkpoint = $
|
|
161
|
+
AND duty_type = $7
|
|
162
|
+
AND block_index_within_checkpoint = $6
|
|
159
163
|
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
160
164
|
`;
|
|
161
165
|
|
|
@@ -253,6 +257,7 @@ SELECT
|
|
|
253
257
|
validator_address,
|
|
254
258
|
slot,
|
|
255
259
|
block_number,
|
|
260
|
+
checkpoint_number,
|
|
256
261
|
block_index_within_checkpoint,
|
|
257
262
|
duty_type,
|
|
258
263
|
status,
|