@aztec/validator-ha-signer 0.0.1-commit.d431d1c → 0.0.1-commit.d939eb5aa
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 +75 -5
- 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 +93 -4
- 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/README.md
CHANGED
|
@@ -36,7 +36,6 @@ import { createHASigner } from '@aztec/validator-ha-signer/factory';
|
|
|
36
36
|
|
|
37
37
|
const { signer, db } = await createHASigner({
|
|
38
38
|
databaseUrl: process.env.DATABASE_URL,
|
|
39
|
-
haSigningEnabled: true,
|
|
40
39
|
nodeId: 'validator-node-1',
|
|
41
40
|
pollingIntervalMs: 100,
|
|
42
41
|
signingTimeoutMs: 3000,
|
|
@@ -81,7 +80,6 @@ const db = new PostgresSlashingProtectionDatabase(pool);
|
|
|
81
80
|
await db.initialize();
|
|
82
81
|
|
|
83
82
|
const signer = new ValidatorHASigner(db, {
|
|
84
|
-
haSigningEnabled: true,
|
|
85
83
|
nodeId: 'validator-node-1',
|
|
86
84
|
pollingIntervalMs: 100,
|
|
87
85
|
signingTimeoutMs: 3000,
|
|
@@ -178,6 +176,16 @@ All signing operations require a `SigningContext` that includes:
|
|
|
178
176
|
|
|
179
177
|
Note: `AUTH_REQUEST` duties bypass HA protection since signing multiple times is safe for authentication requests.
|
|
180
178
|
|
|
179
|
+
## Important Limitations
|
|
180
|
+
|
|
181
|
+
### Database Isolation Per Rollup Version
|
|
182
|
+
|
|
183
|
+
**You cannot use the same database to provide slashing protection for validator nodes running on different rollup versions** (e.g., current rollup and old rollup simultaneously).
|
|
184
|
+
|
|
185
|
+
When the HA signer performs background cleanup via `cleanupOutdatedRollupDuties()`, it removes all duties where the rollup address doesn't match the current rollup address. If two validators running on different rollup versions share the same database, they will delete each other's duties during cleanup.
|
|
186
|
+
|
|
187
|
+
**Solution**: Use separate databases for validators running on different rollup versions. Each rollup version requires its own isolated slashing protection database.
|
|
188
|
+
|
|
181
189
|
## Development
|
|
182
190
|
|
|
183
191
|
```bash
|
package/dest/db/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './types.js';
|
|
2
2
|
export * from './schema.js';
|
|
3
3
|
export * from './postgres.js';
|
|
4
|
-
|
|
4
|
+
export * from './lmdb.js';
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLFlBQVksQ0FBQztBQUMzQixjQUFjLGFBQWEsQ0FBQztBQUM1QixjQUFjLGVBQWUsQ0FBQztBQUM5QixjQUFjLFdBQVcsQ0FBQyJ9
|
package/dest/db/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC"}
|
package/dest/db/index.js
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
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 { EthAddress } from '@aztec/foundation/eth-address';
|
|
12
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
13
|
+
import type { AztecAsyncKVStore } from '@aztec/kv-store';
|
|
14
|
+
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
15
|
+
import { type CheckAndRecordParams, DutyType } from './types.js';
|
|
16
|
+
/**
|
|
17
|
+
* LMDB-backed implementation of SlashingProtectionDatabase.
|
|
18
|
+
*
|
|
19
|
+
* Provides single-node double-signing protection that survives crashes and restarts.
|
|
20
|
+
* Does not provide cross-node coordination (that requires the PostgreSQL implementation).
|
|
21
|
+
*/
|
|
22
|
+
export declare class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
23
|
+
private readonly store;
|
|
24
|
+
private readonly dateProvider;
|
|
25
|
+
static readonly SCHEMA_VERSION = 2;
|
|
26
|
+
private readonly duties;
|
|
27
|
+
private readonly log;
|
|
28
|
+
constructor(store: AztecAsyncKVStore, dateProvider: DateProvider);
|
|
29
|
+
/**
|
|
30
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
31
|
+
*
|
|
32
|
+
* LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
|
|
33
|
+
*/
|
|
34
|
+
tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult>;
|
|
35
|
+
/**
|
|
36
|
+
* Update a duty to 'signed' status with the signature.
|
|
37
|
+
* Only succeeds if the lockToken matches.
|
|
38
|
+
*/
|
|
39
|
+
updateDutySigned(rollupAddress: EthAddress, validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, signature: string, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Delete a duty record.
|
|
42
|
+
* Only succeeds if the lockToken matches.
|
|
43
|
+
*/
|
|
44
|
+
deleteDuty(rollupAddress: EthAddress, validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
47
|
+
*/
|
|
48
|
+
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
49
|
+
/**
|
|
50
|
+
* Cleanup duties with outdated rollup address.
|
|
51
|
+
*
|
|
52
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
53
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
54
|
+
* startup whenever the rollup address changes.
|
|
55
|
+
*/
|
|
56
|
+
cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number>;
|
|
57
|
+
/**
|
|
58
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
59
|
+
*/
|
|
60
|
+
cleanupOldDuties(maxAgeMs: number): Promise<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Close the underlying LMDB store.
|
|
63
|
+
*/
|
|
64
|
+
close(): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG1kYi5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2RiL2xtZGIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7Ozs7O0dBUUc7QUFDSCxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFFN0QsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBRTNELE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQzVELE9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DB 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
|
+
*/ import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
10
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
11
|
+
import { DutyStatus, getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
12
|
+
function dutyKey(rollupAddress, validatorAddress, slot, dutyType, blockIndexWithinCheckpoint) {
|
|
13
|
+
return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* LMDB-backed implementation of SlashingProtectionDatabase.
|
|
17
|
+
*
|
|
18
|
+
* Provides single-node double-signing protection that survives crashes and restarts.
|
|
19
|
+
* Does not provide cross-node coordination (that requires the PostgreSQL implementation).
|
|
20
|
+
*/ export class LmdbSlashingProtectionDatabase {
|
|
21
|
+
store;
|
|
22
|
+
dateProvider;
|
|
23
|
+
static SCHEMA_VERSION = 2;
|
|
24
|
+
duties;
|
|
25
|
+
log;
|
|
26
|
+
constructor(store, dateProvider){
|
|
27
|
+
this.store = store;
|
|
28
|
+
this.dateProvider = dateProvider;
|
|
29
|
+
this.log = createLogger('slashing-protection:lmdb');
|
|
30
|
+
this.duties = store.openMap('signing-protection-duties');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
34
|
+
*
|
|
35
|
+
* LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
|
|
36
|
+
*/ async tryInsertOrGetExisting(params) {
|
|
37
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
38
|
+
const key = dutyKey(params.rollupAddress.toString(), params.validatorAddress.toString(), params.slot.toString(), params.dutyType, blockIndexWithinCheckpoint);
|
|
39
|
+
const lockToken = randomBytes(16).toString('hex');
|
|
40
|
+
const now = this.dateProvider.now();
|
|
41
|
+
const result = await this.store.transactionAsync(async ()=>{
|
|
42
|
+
const existing = await this.duties.getAsync(key);
|
|
43
|
+
if (existing) {
|
|
44
|
+
return {
|
|
45
|
+
isNew: false,
|
|
46
|
+
record: {
|
|
47
|
+
...existing,
|
|
48
|
+
lockToken: ''
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const newRecord = {
|
|
53
|
+
rollupAddress: params.rollupAddress.toString(),
|
|
54
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
55
|
+
slot: params.slot.toString(),
|
|
56
|
+
blockNumber: params.blockNumber.toString(),
|
|
57
|
+
checkpointNumber: params.checkpointNumber.toString(),
|
|
58
|
+
blockIndexWithinCheckpoint,
|
|
59
|
+
dutyType: params.dutyType,
|
|
60
|
+
status: DutyStatus.SIGNING,
|
|
61
|
+
messageHash: params.messageHash,
|
|
62
|
+
nodeId: params.nodeId,
|
|
63
|
+
lockToken,
|
|
64
|
+
startedAtMs: now
|
|
65
|
+
};
|
|
66
|
+
await this.duties.set(key, newRecord);
|
|
67
|
+
return {
|
|
68
|
+
isNew: true,
|
|
69
|
+
record: newRecord
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
if (result.isNew) {
|
|
73
|
+
this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
|
|
74
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
75
|
+
nodeId: params.nodeId
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
isNew: result.isNew,
|
|
80
|
+
record: recordFromFields(result.record)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Update a duty to 'signed' status with the signature.
|
|
85
|
+
* Only succeeds if the lockToken matches.
|
|
86
|
+
*/ updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
87
|
+
const key = dutyKey(rollupAddress.toString(), validatorAddress.toString(), slot.toString(), dutyType, blockIndexWithinCheckpoint);
|
|
88
|
+
return this.store.transactionAsync(async ()=>{
|
|
89
|
+
const existing = await this.duties.getAsync(key);
|
|
90
|
+
if (!existing) {
|
|
91
|
+
this.log.warn('Failed to update duty to signed: duty not found', {
|
|
92
|
+
rollupAddress: rollupAddress.toString(),
|
|
93
|
+
validatorAddress: validatorAddress.toString(),
|
|
94
|
+
slot: slot.toString(),
|
|
95
|
+
dutyType,
|
|
96
|
+
blockIndexWithinCheckpoint
|
|
97
|
+
});
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (existing.lockToken !== lockToken) {
|
|
101
|
+
this.log.warn('Failed to update duty to signed: invalid token', {
|
|
102
|
+
rollupAddress: rollupAddress.toString(),
|
|
103
|
+
validatorAddress: validatorAddress.toString(),
|
|
104
|
+
slot: slot.toString(),
|
|
105
|
+
dutyType,
|
|
106
|
+
blockIndexWithinCheckpoint
|
|
107
|
+
});
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
await this.duties.set(key, {
|
|
111
|
+
...existing,
|
|
112
|
+
status: DutyStatus.SIGNED,
|
|
113
|
+
signature,
|
|
114
|
+
completedAtMs: this.dateProvider.now()
|
|
115
|
+
});
|
|
116
|
+
return true;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Delete a duty record.
|
|
121
|
+
* Only succeeds if the lockToken matches.
|
|
122
|
+
*/ deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
123
|
+
const key = dutyKey(rollupAddress.toString(), validatorAddress.toString(), slot.toString(), dutyType, blockIndexWithinCheckpoint);
|
|
124
|
+
return this.store.transactionAsync(async ()=>{
|
|
125
|
+
const existing = await this.duties.getAsync(key);
|
|
126
|
+
if (!existing || existing.lockToken !== lockToken) {
|
|
127
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
128
|
+
rollupAddress: rollupAddress.toString(),
|
|
129
|
+
validatorAddress: validatorAddress.toString(),
|
|
130
|
+
slot: slot.toString(),
|
|
131
|
+
dutyType,
|
|
132
|
+
blockIndexWithinCheckpoint
|
|
133
|
+
});
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
await this.duties.delete(key);
|
|
137
|
+
return true;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
142
|
+
*/ cleanupOwnStuckDuties(nodeId, maxAgeMs) {
|
|
143
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
144
|
+
return this.store.transactionAsync(async ()=>{
|
|
145
|
+
const keysToDelete = [];
|
|
146
|
+
for await (const [key, record] of this.duties.entriesAsync()){
|
|
147
|
+
if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
|
|
148
|
+
keysToDelete.push(key);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const key of keysToDelete){
|
|
152
|
+
await this.duties.delete(key);
|
|
153
|
+
}
|
|
154
|
+
return keysToDelete.length;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Cleanup duties with outdated rollup address.
|
|
159
|
+
*
|
|
160
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
161
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
162
|
+
* startup whenever the rollup address changes.
|
|
163
|
+
*/ cleanupOutdatedRollupDuties(_currentRollupAddress) {
|
|
164
|
+
return Promise.resolve(0);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
168
|
+
*/ cleanupOldDuties(maxAgeMs) {
|
|
169
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
170
|
+
return this.store.transactionAsync(async ()=>{
|
|
171
|
+
const keysToDelete = [];
|
|
172
|
+
for await (const [key, record] of this.duties.entriesAsync()){
|
|
173
|
+
if (record.status === DutyStatus.SIGNED && record.completedAtMs !== undefined && record.completedAtMs < cutoffMs) {
|
|
174
|
+
keysToDelete.push(key);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (const key of keysToDelete){
|
|
178
|
+
await this.duties.delete(key);
|
|
179
|
+
}
|
|
180
|
+
return keysToDelete.length;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Close the underlying LMDB store.
|
|
185
|
+
*/ async close() {
|
|
186
|
+
await this.store.close();
|
|
187
|
+
this.log.debug('LMDB slashing protection database closed');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
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
|
export declare function up(pgm: MigrationBuilder): void;
|
|
8
10
|
export declare function down(pgm: MigrationBuilder): void;
|
|
9
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
11
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMV9pbml0aWFsLXNjaGVtYS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2RiL21pZ3JhdGlvbnMvMV9pbml0aWFsLXNjaGVtYS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFDSCxPQUFPLEtBQUssRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLGlCQUFpQixDQUFDO0FBaUN4RCx3QkFBZ0IsRUFBRSxDQUFDLEdBQUcsRUFBRSxnQkFBZ0IsR0FBRyxJQUFJLENBVzlDO0FBRUQsd0JBQWdCLElBQUksQ0FBQyxHQUFHLEVBQUUsZ0JBQWdCLEdBQUcsSUFBSSxDQUdoRCJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"1_initial-schema.d.ts","sourceRoot":"","sources":["../../../src/db/migrations/1_initial-schema.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"1_initial-schema.d.ts","sourceRoot":"","sources":["../../../src/db/migrations/1_initial-schema.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAiCxD,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAW9C;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAGhD"}
|
|
@@ -1,16 +1,46 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Initial schema for validator HA slashing protection
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
|
|
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.
|
|
7
|
+
*/ import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE } from '../schema.js';
|
|
8
|
+
// Snapshot of the initial schema — does NOT include checkpoint_number (added in migration 2).
|
|
9
|
+
const INITIAL_SCHEMA_SETUP = [
|
|
10
|
+
`CREATE TABLE IF NOT EXISTS schema_version (
|
|
11
|
+
version INTEGER PRIMARY KEY,
|
|
12
|
+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
13
|
+
);`,
|
|
14
|
+
`CREATE TABLE IF NOT EXISTS validator_duties (
|
|
15
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
16
|
+
validator_address VARCHAR(42) NOT NULL,
|
|
17
|
+
slot BIGINT NOT NULL,
|
|
18
|
+
block_number BIGINT NOT NULL,
|
|
19
|
+
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
20
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
21
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
22
|
+
message_hash VARCHAR(66) NOT NULL,
|
|
23
|
+
signature VARCHAR(132),
|
|
24
|
+
node_id VARCHAR(255) NOT NULL,
|
|
25
|
+
lock_token VARCHAR(64) NOT NULL,
|
|
26
|
+
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
27
|
+
completed_at TIMESTAMP,
|
|
28
|
+
error_message TEXT,
|
|
29
|
+
|
|
30
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
31
|
+
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
32
|
+
);`,
|
|
33
|
+
`CREATE INDEX IF NOT EXISTS idx_validator_duties_status ON validator_duties(status, started_at);`,
|
|
34
|
+
`CREATE INDEX IF NOT EXISTS idx_validator_duties_node ON validator_duties(node_id, started_at);`
|
|
35
|
+
];
|
|
6
36
|
export function up(pgm) {
|
|
7
|
-
for (const statement of
|
|
37
|
+
for (const statement of INITIAL_SCHEMA_SETUP){
|
|
8
38
|
pgm.sql(statement);
|
|
9
39
|
}
|
|
10
40
|
// Insert initial schema version
|
|
11
41
|
pgm.sql(`
|
|
12
42
|
INSERT INTO schema_version (version)
|
|
13
|
-
VALUES (
|
|
43
|
+
VALUES (1)
|
|
14
44
|
ON CONFLICT (version) DO NOTHING;
|
|
15
45
|
`);
|
|
16
46
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add checkpoint_number column to validator_duties table
|
|
3
|
+
*/
|
|
4
|
+
import type { MigrationBuilder } from 'node-pg-migrate';
|
|
5
|
+
export declare function up(pgm: MigrationBuilder): void;
|
|
6
|
+
export declare function down(pgm: MigrationBuilder): void;
|
|
7
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMl9hZGQtY2hlY2twb2ludC1udW1iZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9kYi9taWdyYXRpb25zLzJfYWRkLWNoZWNrcG9pbnQtbnVtYmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsT0FBTyxLQUFLLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUV4RCx3QkFBZ0IsRUFBRSxDQUFDLEdBQUcsRUFBRSxnQkFBZ0IsR0FBRyxJQUFJLENBTzlDO0FBRUQsd0JBQWdCLElBQUksQ0FBQyxHQUFHLEVBQUUsZ0JBQWdCLEdBQUcsSUFBSSxDQUloRCJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"2_add-checkpoint-number.d.ts","sourceRoot":"","sources":["../../../src/db/migrations/2_add-checkpoint-number.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAO9C;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAIhD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add checkpoint_number column to validator_duties table
|
|
3
|
+
*/ export function up(pgm) {
|
|
4
|
+
pgm.addColumn('validator_duties', {
|
|
5
|
+
// eslint-disable-next-line camelcase
|
|
6
|
+
checkpoint_number: {
|
|
7
|
+
type: 'bigint',
|
|
8
|
+
notNull: true,
|
|
9
|
+
default: 0
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
pgm.sql(`UPDATE schema_version SET version = 2 WHERE version = 1`);
|
|
13
|
+
}
|
|
14
|
+
export function down(pgm) {
|
|
15
|
+
pgm.dropColumn('validator_duties', 'checkpoint_number');
|
|
16
|
+
pgm.sql(`UPDATE schema_version SET version = 1 WHERE version = 2`);
|
|
17
|
+
}
|
package/dest/db/postgres.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
44
44
|
*
|
|
45
45
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
46
46
|
*/
|
|
47
|
-
updateDutySigned(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, signature: string, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
47
|
+
updateDutySigned(rollupAddress: EthAddress, validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, signature: string, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
48
48
|
/**
|
|
49
49
|
* Delete a duty record.
|
|
50
50
|
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
@@ -52,9 +52,11 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
52
52
|
*
|
|
53
53
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
54
54
|
*/
|
|
55
|
-
deleteDuty(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
55
|
+
deleteDuty(rollupAddress: EthAddress, validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
56
56
|
/**
|
|
57
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
57
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
58
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
59
|
+
* then delegates to the shared recordFromFields() converter.
|
|
58
60
|
*/
|
|
59
61
|
private rowToRecord;
|
|
60
62
|
/**
|
|
@@ -66,5 +68,19 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
66
68
|
* @returns the number of duties cleaned up
|
|
67
69
|
*/
|
|
68
70
|
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
71
|
+
/**
|
|
72
|
+
* Cleanup duties with outdated rollup address.
|
|
73
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
74
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
75
|
+
* @returns the number of duties cleaned up
|
|
76
|
+
*/
|
|
77
|
+
cleanupOutdatedRollupDuties(currentRollupAddress: EthAddress): Promise<number>;
|
|
78
|
+
/**
|
|
79
|
+
* Cleanup old signed duties.
|
|
80
|
+
* Removes only signed duties older than the specified age.
|
|
81
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
82
|
+
* @returns the number of duties cleaned up
|
|
83
|
+
*/
|
|
84
|
+
cleanupOldDuties(maxAgeMs: number): Promise<number>;
|
|
69
85
|
}
|
|
70
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
86
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zdGdyZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi9wb3N0Z3Jlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUNILE9BQ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jC,OAAO,CAAC,OAAO,CAAC,CAsBlB;IAED;;;;;;OAMG;IACG,UAAU,CACd,aAAa,EAAE,UAAU,EACzB,gBAAgB,EAAE,UAAU,EAC5B,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,0BAA0B,EAAE,MAAM,GACjC,OAAO,CAAC,OAAO,CAAC,CAqBlB;IAED;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAoBnB;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAG3B;IAED;;;OAGG;IACG,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG7E;IAED;;;;;OAKG;IACG,2BAA2B,CAAC,oBAAoB,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAGnF;IAED;;;;;OAKG;IACG,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGxD;CACF"}
|
package/dest/db/postgres.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
|
-
*/ import {
|
|
4
|
-
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
5
|
-
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
|
+
*/ import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
6
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
7
5
|
import { makeBackoff, retry } from '@aztec/foundation/retry';
|
|
8
|
-
import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSION, UPDATE_DUTY_SIGNED } from './schema.js';
|
|
9
|
-
import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
6
|
+
import { CLEANUP_OLD_DUTIES, CLEANUP_OUTDATED_ROLLUP_DUTIES, CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSION, UPDATE_DUTY_SIGNED } from './schema.js';
|
|
7
|
+
import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
10
8
|
/**
|
|
11
9
|
* PostgreSQL implementation of the slashing protection database
|
|
12
10
|
*/ export class PostgresSlashingProtectionDatabase {
|
|
@@ -63,9 +61,11 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
63
61
|
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
64
62
|
const result = await retry(async ()=>{
|
|
65
63
|
const queryResult = await this.pool.query(INSERT_OR_GET_DUTY, [
|
|
64
|
+
params.rollupAddress.toString(),
|
|
66
65
|
params.validatorAddress.toString(),
|
|
67
66
|
params.slot.toString(),
|
|
68
67
|
params.blockNumber.toString(),
|
|
68
|
+
params.checkpointNumber.toString(),
|
|
69
69
|
blockIndexWithinCheckpoint,
|
|
70
70
|
params.dutyType,
|
|
71
71
|
params.messageHash,
|
|
@@ -97,9 +97,10 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
97
97
|
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
98
98
|
*
|
|
99
99
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
100
|
-
*/ async updateDutySigned(validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
100
|
+
*/ async updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
101
101
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
102
102
|
signature,
|
|
103
|
+
rollupAddress.toString(),
|
|
103
104
|
validatorAddress.toString(),
|
|
104
105
|
slot.toString(),
|
|
105
106
|
dutyType,
|
|
@@ -108,6 +109,7 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
108
109
|
]);
|
|
109
110
|
if (result.rowCount === 0) {
|
|
110
111
|
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
112
|
+
rollupAddress: rollupAddress.toString(),
|
|
111
113
|
validatorAddress: validatorAddress.toString(),
|
|
112
114
|
slot: slot.toString(),
|
|
113
115
|
dutyType,
|
|
@@ -123,8 +125,9 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
123
125
|
* Used when signing fails to allow another node/attempt to retry.
|
|
124
126
|
*
|
|
125
127
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
126
|
-
*/ async deleteDuty(validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
128
|
+
*/ async deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
127
129
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
130
|
+
rollupAddress.toString(),
|
|
128
131
|
validatorAddress.toString(),
|
|
129
132
|
slot.toString(),
|
|
130
133
|
dutyType,
|
|
@@ -133,6 +136,7 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
133
136
|
]);
|
|
134
137
|
if (result.rowCount === 0) {
|
|
135
138
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
139
|
+
rollupAddress: rollupAddress.toString(),
|
|
136
140
|
validatorAddress: validatorAddress.toString(),
|
|
137
141
|
slot: slot.toString(),
|
|
138
142
|
dutyType,
|
|
@@ -143,12 +147,16 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
143
147
|
return true;
|
|
144
148
|
}
|
|
145
149
|
/**
|
|
146
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
150
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
151
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
152
|
+
* then delegates to the shared recordFromFields() converter.
|
|
147
153
|
*/ rowToRecord(row) {
|
|
148
|
-
return {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
return recordFromFields({
|
|
155
|
+
rollupAddress: row.rollup_address,
|
|
156
|
+
validatorAddress: row.validator_address,
|
|
157
|
+
slot: row.slot,
|
|
158
|
+
blockNumber: row.block_number,
|
|
159
|
+
checkpointNumber: row.checkpoint_number,
|
|
152
160
|
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
153
161
|
dutyType: row.duty_type,
|
|
154
162
|
status: row.status,
|
|
@@ -156,10 +164,10 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
156
164
|
signature: row.signature ?? undefined,
|
|
157
165
|
nodeId: row.node_id,
|
|
158
166
|
lockToken: row.lock_token,
|
|
159
|
-
|
|
160
|
-
|
|
167
|
+
startedAtMs: row.started_at.getTime(),
|
|
168
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
161
169
|
errorMessage: row.error_message ?? undefined
|
|
162
|
-
};
|
|
170
|
+
});
|
|
163
171
|
}
|
|
164
172
|
/**
|
|
165
173
|
* Close the database connection pool
|
|
@@ -171,10 +179,31 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
171
179
|
* Cleanup own stuck duties
|
|
172
180
|
* @returns the number of duties cleaned up
|
|
173
181
|
*/ async cleanupOwnStuckDuties(nodeId, maxAgeMs) {
|
|
174
|
-
const cutoff = new Date(Date.now() - maxAgeMs);
|
|
175
182
|
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [
|
|
176
183
|
nodeId,
|
|
177
|
-
|
|
184
|
+
maxAgeMs
|
|
185
|
+
]);
|
|
186
|
+
return result.rowCount ?? 0;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Cleanup duties with outdated rollup address.
|
|
190
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
191
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
192
|
+
* @returns the number of duties cleaned up
|
|
193
|
+
*/ async cleanupOutdatedRollupDuties(currentRollupAddress) {
|
|
194
|
+
const result = await this.pool.query(CLEANUP_OUTDATED_ROLLUP_DUTIES, [
|
|
195
|
+
currentRollupAddress.toString()
|
|
196
|
+
]);
|
|
197
|
+
return result.rowCount ?? 0;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Cleanup old signed duties.
|
|
201
|
+
* Removes only signed duties older than the specified age.
|
|
202
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
203
|
+
* @returns the number of duties cleaned up
|
|
204
|
+
*/ async cleanupOldDuties(maxAgeMs) {
|
|
205
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [
|
|
206
|
+
maxAgeMs
|
|
178
207
|
]);
|
|
179
208
|
return result.rowCount ?? 0;
|
|
180
209
|
}
|