@aztec/validator-ha-signer 0.0.1-commit.3469e52 → 0.0.1-commit.3895657bc
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 +188 -0
- package/dest/db/postgres.d.ts +20 -4
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +44 -17
- package/dest/db/schema.d.ts +17 -10
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +39 -22
- package/dest/db/types.d.ts +43 -19
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +30 -15
- package/dest/factory.d.ts +22 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +50 -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 +55 -15
- package/dest/types.d.ts +32 -72
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +3 -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 +24 -11
- package/package.json +10 -5
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +264 -0
- package/src/db/postgres.ts +45 -12
- package/src/db/schema.ts +41 -22
- package/src/db/types.ts +67 -17
- package/src/factory.ts +61 -4
- package/src/metrics.ts +138 -0
- package/src/slashing_protection_service.ts +77 -19
- package/src/types.ts +50 -103
- package/src/validator_ha_signer.ts +41 -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 = 1;
|
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG1kYi5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2RiL2xtZGIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7Ozs7O0dBUUc7QUFDSCxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFFN0QsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBRTNELE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQzVELE9BQU8sS0FBSyxFQUFFLGlCQUFpQixFQUFpQixNQUFNLGlCQUFpQixDQUFDO0FBRXhFLE9BQU8sS0FBSyxFQUFFLDBCQUEwQixFQUFFLG9CQUFvQixFQUFFLE1BQU0sYUFBYSxDQUFDO0FBQ3BGLE9BQU8sRUFDTCxLQUFLLG9CQUFvQixFQUV6QixRQUFRLEVBSVQsTUFBTSxZQUFZLENBQUM7QUFZcEI7Ozs7O0dBS0c7QUFDSCxxQkFBYSw4QkF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AAO,CAAC,oBAAoB,CAAC,CA4C/F;IAED;;;OAGG;IACI,gBAAgB,CACrB,aAAa,EAAE,UAAU,EACzB,gBAAgB,EAAE,UAAU,EAC5B,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,0BAA0B,EAAE,MAAM,GACjC,OAAO,CAAC,OAAO,CAAC,CA0ClB;IAED;;;OAGG;IACI,UAAU,CACf,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,CAyBlB;IAED;;OAEG;IACI,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAe9E;IAED;;;;;;OAMG;IACI,2BAA2B,CAAC,qBAAqB,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAErF;IAED;;OAEG;IACI,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAmBzD;IAED;;OAEG;IACU,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAGlC;CACF"}
|
package/dest/db/lmdb.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
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
|
+
*/ 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 = 1;
|
|
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
|
+
blockIndexWithinCheckpoint,
|
|
58
|
+
dutyType: params.dutyType,
|
|
59
|
+
status: DutyStatus.SIGNING,
|
|
60
|
+
messageHash: params.messageHash,
|
|
61
|
+
nodeId: params.nodeId,
|
|
62
|
+
lockToken,
|
|
63
|
+
startedAtMs: now
|
|
64
|
+
};
|
|
65
|
+
await this.duties.set(key, newRecord);
|
|
66
|
+
return {
|
|
67
|
+
isNew: true,
|
|
68
|
+
record: newRecord
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
if (result.isNew) {
|
|
72
|
+
this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
|
|
73
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
74
|
+
nodeId: params.nodeId
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
isNew: result.isNew,
|
|
79
|
+
record: recordFromFields(result.record)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Update a duty to 'signed' status with the signature.
|
|
84
|
+
* Only succeeds if the lockToken matches.
|
|
85
|
+
*/ updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
86
|
+
const key = dutyKey(rollupAddress.toString(), validatorAddress.toString(), slot.toString(), dutyType, blockIndexWithinCheckpoint);
|
|
87
|
+
return this.store.transactionAsync(async ()=>{
|
|
88
|
+
const existing = await this.duties.getAsync(key);
|
|
89
|
+
if (!existing) {
|
|
90
|
+
this.log.warn('Failed to update duty to signed: duty not found', {
|
|
91
|
+
rollupAddress: rollupAddress.toString(),
|
|
92
|
+
validatorAddress: validatorAddress.toString(),
|
|
93
|
+
slot: slot.toString(),
|
|
94
|
+
dutyType,
|
|
95
|
+
blockIndexWithinCheckpoint
|
|
96
|
+
});
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
if (existing.lockToken !== lockToken) {
|
|
100
|
+
this.log.warn('Failed to update duty to signed: invalid token', {
|
|
101
|
+
rollupAddress: rollupAddress.toString(),
|
|
102
|
+
validatorAddress: validatorAddress.toString(),
|
|
103
|
+
slot: slot.toString(),
|
|
104
|
+
dutyType,
|
|
105
|
+
blockIndexWithinCheckpoint
|
|
106
|
+
});
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
await this.duties.set(key, {
|
|
110
|
+
...existing,
|
|
111
|
+
status: DutyStatus.SIGNED,
|
|
112
|
+
signature,
|
|
113
|
+
completedAtMs: this.dateProvider.now()
|
|
114
|
+
});
|
|
115
|
+
return true;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Delete a duty record.
|
|
120
|
+
* Only succeeds if the lockToken matches.
|
|
121
|
+
*/ deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
122
|
+
const key = dutyKey(rollupAddress.toString(), validatorAddress.toString(), slot.toString(), dutyType, blockIndexWithinCheckpoint);
|
|
123
|
+
return this.store.transactionAsync(async ()=>{
|
|
124
|
+
const existing = await this.duties.getAsync(key);
|
|
125
|
+
if (!existing || existing.lockToken !== lockToken) {
|
|
126
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
127
|
+
rollupAddress: rollupAddress.toString(),
|
|
128
|
+
validatorAddress: validatorAddress.toString(),
|
|
129
|
+
slot: slot.toString(),
|
|
130
|
+
dutyType,
|
|
131
|
+
blockIndexWithinCheckpoint
|
|
132
|
+
});
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
await this.duties.delete(key);
|
|
136
|
+
return true;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
141
|
+
*/ cleanupOwnStuckDuties(nodeId, maxAgeMs) {
|
|
142
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
143
|
+
return this.store.transactionAsync(async ()=>{
|
|
144
|
+
const keysToDelete = [];
|
|
145
|
+
for await (const [key, record] of this.duties.entriesAsync()){
|
|
146
|
+
if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
|
|
147
|
+
keysToDelete.push(key);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
for (const key of keysToDelete){
|
|
151
|
+
await this.duties.delete(key);
|
|
152
|
+
}
|
|
153
|
+
return keysToDelete.length;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Cleanup duties with outdated rollup address.
|
|
158
|
+
*
|
|
159
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
160
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
161
|
+
* startup whenever the rollup address changes.
|
|
162
|
+
*/ cleanupOutdatedRollupDuties(_currentRollupAddress) {
|
|
163
|
+
return Promise.resolve(0);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
167
|
+
*/ cleanupOldDuties(maxAgeMs) {
|
|
168
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
169
|
+
return this.store.transactionAsync(async ()=>{
|
|
170
|
+
const keysToDelete = [];
|
|
171
|
+
for await (const [key, record] of this.duties.entriesAsync()){
|
|
172
|
+
if (record.status === DutyStatus.SIGNED && record.completedAtMs !== undefined && record.completedAtMs < cutoffMs) {
|
|
173
|
+
keysToDelete.push(key);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const key of keysToDelete){
|
|
177
|
+
await this.duties.delete(key);
|
|
178
|
+
}
|
|
179
|
+
return keysToDelete.length;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Close the underlying LMDB store.
|
|
184
|
+
*/ async close() {
|
|
185
|
+
await this.store.close();
|
|
186
|
+
this.log.debug('LMDB slashing protection database closed');
|
|
187
|
+
}
|
|
188
|
+
}
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zdGdyZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi9wb3N0Z3Jlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUNILE9BQU8sRUFBRSxVQUFVLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUU3RCxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFJM0QsT0FBTyxLQUFLLEVBQUUsV0FBVyxFQUFFLGNBQWMsRUFBRSxNQUFNLElBQUksQ0FBQztBQUV0RCxPQUFPLEtBQUssRUFBRSwwQkFBMEIsRU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sQ0FBQyxPQUFPLENBQUMsQ0FzQmxCO0lBRUQ7Ozs7OztPQU1HO0lBQ0csVUFBVSxDQUNkLGFBQWEsRUFBRSxVQUFVLEVBQ3pCLGdCQUFnQixFQUFFLFVBQVUsRUFDNUIsSUFBSSxFQUFFLFVBQVUsRUFDaEIsUUFBUSxFQUFFLFFBQVEsRUFDbEIsU0FBUyxFQUFFLE1BQU0sRUFDakIsMEJBQTBCLEVBQUUsTUFBTSxHQUNqQyxPQUFPLENBQUMsT0FBTyxDQUFDLENBcUJsQjtJQUVEOzs7O09BSUc7SUFDSCxPQUFPLENBQUMsV0FBVztJQW1CbkI7O09BRUc7SUFDRyxLQUFLLElBQUksT0FBTyxDQUFDLElBQUksQ0FBQyxDQUczQjtJQUVEOzs7T0FHRztJQUNHLHFCQUFxQixDQUFDLE1BQU0sRUFBRSxNQUFNLEVBQUUsUUFBUSxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDLENBRzdFO0lBRUQ7Ozs7O09BS0c7SUFDRywyQkFBMkIsQ0FBQyxvQkFBb0IsRUFBRSxVQUFVLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQUduRjtJQUVEOzs7OztPQUtHO0lBQ0csZ0JBQWdCLENBQUMsUUFBUSxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDLENBR3hEO0NBQ0YifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/db/postgres.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/db/postgres.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAI3D,OAAO,KAAK,EAAE,WAAW,EAA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ED;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAmBnB;;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,6 +61,7 @@ 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(),
|
|
@@ -97,9 +96,10 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
97
96
|
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
98
97
|
*
|
|
99
98
|
* @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) {
|
|
99
|
+
*/ async updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
101
100
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
102
101
|
signature,
|
|
102
|
+
rollupAddress.toString(),
|
|
103
103
|
validatorAddress.toString(),
|
|
104
104
|
slot.toString(),
|
|
105
105
|
dutyType,
|
|
@@ -108,6 +108,7 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
108
108
|
]);
|
|
109
109
|
if (result.rowCount === 0) {
|
|
110
110
|
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
111
|
+
rollupAddress: rollupAddress.toString(),
|
|
111
112
|
validatorAddress: validatorAddress.toString(),
|
|
112
113
|
slot: slot.toString(),
|
|
113
114
|
dutyType,
|
|
@@ -123,8 +124,9 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
123
124
|
* Used when signing fails to allow another node/attempt to retry.
|
|
124
125
|
*
|
|
125
126
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
126
|
-
*/ async deleteDuty(validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
127
|
+
*/ async deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
127
128
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
129
|
+
rollupAddress.toString(),
|
|
128
130
|
validatorAddress.toString(),
|
|
129
131
|
slot.toString(),
|
|
130
132
|
dutyType,
|
|
@@ -133,6 +135,7 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
133
135
|
]);
|
|
134
136
|
if (result.rowCount === 0) {
|
|
135
137
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
138
|
+
rollupAddress: rollupAddress.toString(),
|
|
136
139
|
validatorAddress: validatorAddress.toString(),
|
|
137
140
|
slot: slot.toString(),
|
|
138
141
|
dutyType,
|
|
@@ -143,12 +146,15 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
143
146
|
return true;
|
|
144
147
|
}
|
|
145
148
|
/**
|
|
146
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
149
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
150
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
151
|
+
* then delegates to the shared recordFromFields() converter.
|
|
147
152
|
*/ rowToRecord(row) {
|
|
148
|
-
return {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
153
|
+
return recordFromFields({
|
|
154
|
+
rollupAddress: row.rollup_address,
|
|
155
|
+
validatorAddress: row.validator_address,
|
|
156
|
+
slot: row.slot,
|
|
157
|
+
blockNumber: row.block_number,
|
|
152
158
|
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
153
159
|
dutyType: row.duty_type,
|
|
154
160
|
status: row.status,
|
|
@@ -156,10 +162,10 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
156
162
|
signature: row.signature ?? undefined,
|
|
157
163
|
nodeId: row.node_id,
|
|
158
164
|
lockToken: row.lock_token,
|
|
159
|
-
|
|
160
|
-
|
|
165
|
+
startedAtMs: row.started_at.getTime(),
|
|
166
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
161
167
|
errorMessage: row.error_message ?? undefined
|
|
162
|
-
};
|
|
168
|
+
});
|
|
163
169
|
}
|
|
164
170
|
/**
|
|
165
171
|
* Close the database connection pool
|
|
@@ -171,10 +177,31 @@ import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
|
171
177
|
* Cleanup own stuck duties
|
|
172
178
|
* @returns the number of duties cleaned up
|
|
173
179
|
*/ async cleanupOwnStuckDuties(nodeId, maxAgeMs) {
|
|
174
|
-
const cutoff = new Date(Date.now() - maxAgeMs);
|
|
175
180
|
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [
|
|
176
181
|
nodeId,
|
|
177
|
-
|
|
182
|
+
maxAgeMs
|
|
183
|
+
]);
|
|
184
|
+
return result.rowCount ?? 0;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Cleanup duties with outdated rollup address.
|
|
188
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
189
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
190
|
+
* @returns the number of duties cleaned up
|
|
191
|
+
*/ async cleanupOutdatedRollupDuties(currentRollupAddress) {
|
|
192
|
+
const result = await this.pool.query(CLEANUP_OUTDATED_ROLLUP_DUTIES, [
|
|
193
|
+
currentRollupAddress.toString()
|
|
194
|
+
]);
|
|
195
|
+
return result.rowCount ?? 0;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Cleanup old signed duties.
|
|
199
|
+
* Removes only signed duties older than the specified age.
|
|
200
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
201
|
+
* @returns the number of duties cleaned up
|
|
202
|
+
*/ async cleanupOldDuties(maxAgeMs) {
|
|
203
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [
|
|
204
|
+
maxAgeMs
|
|
178
205
|
]);
|
|
179
206
|
return result.rowCount ?? 0;
|
|
180
207
|
}
|
package/dest/db/schema.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare const SCHEMA_VERSION = 1;
|
|
|
12
12
|
/**
|
|
13
13
|
* SQL to create the validator_duties table
|
|
14
14
|
*/
|
|
15
|
-
export declare const CREATE_VALIDATOR_DUTIES_TABLE = "\nCREATE TABLE IF NOT EXISTS validator_duties (\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,\n duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed'
|
|
15
|
+
export declare const CREATE_VALIDATOR_DUTIES_TABLE = "\nCREATE TABLE IF NOT EXISTS validator_duties (\n rollup_address VARCHAR(42) NOT NULL,\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,\n duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),\n message_hash VARCHAR(66) NOT NULL,\n signature VARCHAR(132),\n node_id VARCHAR(255) NOT NULL,\n lock_token VARCHAR(64) NOT NULL,\n started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n completed_at TIMESTAMP,\n error_message TEXT,\n\n PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),\n CHECK (completed_at IS NULL OR completed_at >= started_at)\n);\n";
|
|
16
16
|
/**
|
|
17
17
|
* SQL to create index on status and started_at for cleanup queries
|
|
18
18
|
*/
|
|
@@ -32,7 +32,7 @@ export declare const INSERT_SCHEMA_VERSION = "\nINSERT INTO schema_version (vers
|
|
|
32
32
|
/**
|
|
33
33
|
* Complete schema setup - all statements in order
|
|
34
34
|
*/
|
|
35
|
-
export declare const SCHEMA_SETUP: readonly ["\nCREATE TABLE IF NOT EXISTS schema_version (\n version INTEGER PRIMARY KEY,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n);\n", "\nCREATE TABLE IF NOT EXISTS validator_duties (\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,\n duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed'
|
|
35
|
+
export declare const SCHEMA_SETUP: readonly ["\nCREATE TABLE IF NOT EXISTS schema_version (\n version INTEGER PRIMARY KEY,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n);\n", "\nCREATE TABLE IF NOT EXISTS validator_duties (\n rollup_address VARCHAR(42) NOT NULL,\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,\n duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),\n message_hash VARCHAR(66) NOT NULL,\n signature VARCHAR(132),\n node_id VARCHAR(255) NOT NULL,\n lock_token VARCHAR(64) NOT NULL,\n started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n completed_at TIMESTAMP,\n error_message TEXT,\n\n PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),\n CHECK (completed_at IS NULL OR completed_at >= started_at)\n);\n", "\nCREATE INDEX IF NOT EXISTS idx_validator_duties_status\nON validator_duties(status, started_at);\n", "\nCREATE INDEX IF NOT EXISTS idx_validator_duties_node\nON validator_duties(node_id, started_at);\n"];
|
|
36
36
|
/**
|
|
37
37
|
* Query to get current schema version
|
|
38
38
|
*/
|
|
@@ -48,16 +48,16 @@ export declare const GET_SCHEMA_VERSION = "\nSELECT version FROM schema_version
|
|
|
48
48
|
* just committed the row, there's a small window where the SELECT might not see it yet.
|
|
49
49
|
* The application layer should retry if no rows are returned.
|
|
50
50
|
*/
|
|
51
|
-
export declare const INSERT_OR_GET_DUTY = "\nWITH inserted AS (\n INSERT INTO validator_duties (\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n lock_token,\n started_at\n ) VALUES ($1, $2, $3, $4, $5, 'signing', $
|
|
51
|
+
export declare const INSERT_OR_GET_DUTY = "\nWITH inserted AS (\n INSERT INTO validator_duties (\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n lock_token,\n started_at\n ) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)\n ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING\n RETURNING\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n signature,\n node_id,\n lock_token,\n started_at,\n completed_at,\n error_message,\n TRUE as is_new\n)\nSELECT * FROM inserted\nUNION ALL\nSELECT\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n signature,\n node_id,\n '' as lock_token,\n started_at,\n completed_at,\n error_message,\n FALSE as is_new\nFROM validator_duties\nWHERE rollup_address = $1\n AND validator_address = $2\n AND slot = $3\n AND duty_type = $6\n AND block_index_within_checkpoint = $5\n AND NOT EXISTS (SELECT 1 FROM inserted);\n";
|
|
52
52
|
/**
|
|
53
53
|
* Query to update a duty to 'signed' status
|
|
54
54
|
*/
|
|
55
|
-
export declare const UPDATE_DUTY_SIGNED = "\nUPDATE validator_duties\nSET status = 'signed',\n signature = $1,\n completed_at = CURRENT_TIMESTAMP\nWHERE
|
|
55
|
+
export declare const UPDATE_DUTY_SIGNED = "\nUPDATE validator_duties\nSET status = 'signed',\n signature = $1,\n completed_at = CURRENT_TIMESTAMP\nWHERE rollup_address = $2\n AND validator_address = $3\n AND slot = $4\n AND duty_type = $5\n AND block_index_within_checkpoint = $6\n AND status = 'signing'\n AND lock_token = $7;\n";
|
|
56
56
|
/**
|
|
57
57
|
* Query to delete a duty
|
|
58
58
|
* Only deletes if the lockToken matches
|
|
59
59
|
*/
|
|
60
|
-
export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE
|
|
60
|
+
export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE rollup_address = $1\n AND validator_address = $2\n AND slot = $3\n AND duty_type = $4\n AND block_index_within_checkpoint = $5\n AND status = 'signing'\n AND lock_token = $6;\n";
|
|
61
61
|
/**
|
|
62
62
|
* Query to clean up old signed duties (for maintenance)
|
|
63
63
|
* Removes signed duties older than a specified timestamp
|
|
@@ -65,14 +65,21 @@ export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE valida
|
|
|
65
65
|
export declare const CLEANUP_OLD_SIGNED_DUTIES = "\nDELETE FROM validator_duties\nWHERE status = 'signed'\n AND completed_at < $1;\n";
|
|
66
66
|
/**
|
|
67
67
|
* Query to clean up old duties (for maintenance)
|
|
68
|
-
* Removes duties older than a specified
|
|
68
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
69
69
|
*/
|
|
70
|
-
export declare const CLEANUP_OLD_DUTIES = "\nDELETE FROM validator_duties\nWHERE status
|
|
70
|
+
export declare const CLEANUP_OLD_DUTIES = "\nDELETE FROM validator_duties\nWHERE status = 'signed'\n AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;\n";
|
|
71
71
|
/**
|
|
72
72
|
* Query to cleanup own stuck duties
|
|
73
73
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
74
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
74
75
|
*/
|
|
75
|
-
export declare const CLEANUP_OWN_STUCK_DUTIES = "\nDELETE FROM validator_duties\nWHERE node_id = $1\n AND status = 'signing'\n AND started_at < $2;\n";
|
|
76
|
+
export declare const CLEANUP_OWN_STUCK_DUTIES = "\nDELETE FROM validator_duties\nWHERE node_id = $1\n AND status = 'signing'\n AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;\n";
|
|
77
|
+
/**
|
|
78
|
+
* Query to cleanup duties with outdated rollup address
|
|
79
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
80
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
81
|
+
*/
|
|
82
|
+
export declare const CLEANUP_OUTDATED_ROLLUP_DUTIES = "\nDELETE FROM validator_duties\nWHERE rollup_address != $1;\n";
|
|
76
83
|
/**
|
|
77
84
|
* SQL to drop the validator_duties table
|
|
78
85
|
*/
|
|
@@ -85,5 +92,5 @@ export declare const DROP_SCHEMA_VERSION_TABLE = "DROP TABLE IF EXISTS schema_ve
|
|
|
85
92
|
* Query to get stuck duties (for monitoring/alerting)
|
|
86
93
|
* Returns duties in 'signing' status that have been stuck for too long
|
|
87
94
|
*/
|
|
88
|
-
export declare const GET_STUCK_DUTIES = "\nSELECT\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n started_at,\n EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds\nFROM validator_duties\nWHERE status = 'signing'\n AND started_at < $1\nORDER BY started_at ASC;\n";
|
|
89
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
95
|
+
export declare const GET_STUCK_DUTIES = "\nSELECT\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n started_at,\n EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds\nFROM validator_duties\nWHERE status = 'signing'\n AND started_at < $1\nORDER BY started_at ASC;\n";
|
|
96
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NoZW1hLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGIvc2NoZW1hLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGNBQWMsSUFBSSxDQUFDO0FBRWhDOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDZCQUE2QixzNUJBb0J6QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sbUJBQW1CLHlHQUcvQixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0saUJBQWlCLHdHQUc3QixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sMkJBQTJCLG1KQUt2QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0scUJBQXFCLDZGQUlqQyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sWUFBWSxpd0NBS2YsQ0FBQztBQUVYOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGtCQUFrQiwwRUFFOUIsQ0FBQztBQUVGOzs7Ozs7Ozs7O0dBVUc7QUFDSCxlQUFPLE1BQU0sa0JBQWtCLDZ1Q0EwRDlCLENBQUM7QUFFRjs7R0FFRztBQUNILGVBQU8sTUFBTSxrQkFBa0IsK1NBWTlCLENBQUM7QUFFRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sV0FBVyxpT0FTdkIsQ0FBQztBQUVGOzs7R0FHRztBQUNILGVBQU8sTUFBTSx5QkFBeUIsd0ZBSXJDLENBQUM7QUFFRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sa0JBQWtCLHlJQUk5QixDQUFDO0FBRUY7Ozs7R0FJRztBQUNILGVBQU8sTUFBTSx3QkFBd0IsOEpBS3BDLENBQUM7QUFFRjs7OztHQUlHO0FBQ0gsZUFBTyxNQUFNLDhCQUE4QixrRUFHMUMsQ0FBQztBQUVGOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDJCQUEyQiwyQ0FBMkMsQ0FBQztBQUVwRjs7R0FFRztBQUNILGVBQU8sTUFBTSx5QkFBeUIseUNBQXlDLENBQUM7QUFFaEY7OztHQUdHO0FBQ0gsZUFBTyxNQUFNLGdCQUFnQixrV0FpQjVCLENBQUMifQ==
|
package/dest/db/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/db/schema.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,IAAI,CAAC;AAEhC;;GAEG;AACH,eAAO,MAAM,6BAA6B,
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/db/schema.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,IAAI,CAAC;AAEhC;;GAEG;AACH,eAAO,MAAM,6BAA6B,s5BAoBzC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,yGAG/B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,wGAG7B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,mJAKvC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,qBAAqB,6FAIjC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,iwCAKf,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,kBAAkB,0EAE9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,6uCA0D9B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,+SAY9B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,iOASvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,wFAIrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,yIAI9B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,8JAKpC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,kEAG1C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,2CAA2C,CAAC;AAEpF;;GAEG;AACH,eAAO,MAAM,yBAAyB,yCAAyC,CAAC;AAEhF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,kWAiB5B,CAAC"}
|