@aztec/validator-ha-signer 0.0.1-commit.7cf39cb55 → 0.0.1-commit.7ffbba4
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 +4 -2
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +15 -17
- package/dest/db/schema.d.ts +5 -4
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +4 -3
- package/dest/db/types.d.ts +37 -18
- 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 +12 -3
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +15 -4
- package/dest/types.d.ts +17 -70
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +3 -20
- package/dest/validator_ha_signer.d.ts +12 -4
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +16 -8
- package/package.json +10 -6
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +264 -0
- package/src/db/postgres.ts +15 -15
- package/src/db/schema.ts +4 -3
- package/src/db/types.ts +61 -16
- package/src/factory.ts +61 -4
- package/src/metrics.ts +138 -0
- package/src/slashing_protection_service.ts +26 -5
- package/src/types.ts +32 -104
- package/src/validator_ha_signer.ts +33 -12
- package/dest/config.d.ts +0 -101
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -92
- package/src/config.ts +0 -149
package/dest/types.js
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DutyType } from './db/types.js';
|
|
1
|
+
import { getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib, isHAProtectedContext } from '@aztec/stdlib/ha-signing';
|
|
3
2
|
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
|
|
7
|
-
*/ export function isHAProtectedContext(context) {
|
|
8
|
-
return context.dutyType !== DutyType.AUTH_REQUEST && context.dutyType !== DutyType.TXS;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Gets the block number from a signing context.
|
|
12
|
-
* - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
|
|
13
|
-
* - Other duties: returns the blockNumber from the context
|
|
14
|
-
*/ export function getBlockNumberFromSigningContext(context) {
|
|
15
|
-
// Check for duty types that have blockNumber
|
|
16
|
-
if (context.dutyType === DutyType.BLOCK_PROPOSAL || context.dutyType === DutyType.CHECKPOINT_PROPOSAL || context.dutyType === DutyType.ATTESTATION || context.dutyType === DutyType.ATTESTATIONS_AND_SIGNERS) {
|
|
17
|
-
return context.blockNumber;
|
|
18
|
-
}
|
|
19
|
-
// Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE) don't have blockNumber
|
|
20
|
-
return BlockNumber(0);
|
|
21
|
-
}
|
|
3
|
+
export { isHAProtectedContext };
|
|
4
|
+
export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
|
|
@@ -8,8 +8,14 @@
|
|
|
8
8
|
import type { Buffer32 } from '@aztec/foundation/buffer';
|
|
9
9
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
10
10
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
11
|
-
import type {
|
|
12
|
-
import { type
|
|
11
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
12
|
+
import { type BaseSignerConfig, type HAProtectedSigningContext } from '@aztec/stdlib/ha-signing';
|
|
13
|
+
import type { HASignerMetrics } from './metrics.js';
|
|
14
|
+
import type { SlashingProtectionDatabase } from './types.js';
|
|
15
|
+
export interface ValidatorHASignerDeps {
|
|
16
|
+
metrics: HASignerMetrics;
|
|
17
|
+
dateProvider: DateProvider;
|
|
18
|
+
}
|
|
13
19
|
/**
|
|
14
20
|
* Validator High Availability Signer
|
|
15
21
|
*
|
|
@@ -34,7 +40,9 @@ export declare class ValidatorHASigner {
|
|
|
34
40
|
private readonly log;
|
|
35
41
|
private readonly slashingProtection;
|
|
36
42
|
private readonly rollupAddress;
|
|
37
|
-
|
|
43
|
+
private readonly dateProvider;
|
|
44
|
+
private readonly metrics;
|
|
45
|
+
constructor(db: SlashingProtectionDatabase, config: BaseSignerConfig, deps: ValidatorHASignerDeps);
|
|
38
46
|
/**
|
|
39
47
|
* Sign a message with slashing protection.
|
|
40
48
|
*
|
|
@@ -68,4 +76,4 @@ export declare class ValidatorHASigner {
|
|
|
68
76
|
*/
|
|
69
77
|
stop(): Promise<void>;
|
|
70
78
|
}
|
|
71
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
79
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdG9yX2hhX3NpZ25lci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3ZhbGlkYXRvcl9oYV9zaWduZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBQ0gsT0FBTyxLQUFLLEVBQUUsUUFBUSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDekQsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBQzNELE9BQU8sS0FBSyxFQUFFLFNBQVMsRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBRWpFLE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQzVELE9BQU8sRUFDTCxLQUFLLGdCQUFnQixFQUVyQixLQUFLLHlCQUF5QixFQUUvQixNQUFNLDBCQUEwQixDQUFDO0FBR2xDLE9BQU8sS0FBSyxFQUFFLGVBQWUsRUFBRSxNQUFNLGNBQWMsQ0FBQztBQUVwRCxPQUFPLEtBQUssRUFBRSwwQkFBMEIsRUFBRSxNQUFNLFlBQVksQ0FBQztBQUU3RCxNQUFNLFdBQVcscUJBQXFCO0lBQ3BDLE9BQU8sRUFBRSxlQUFlLENBQUM7SUFDekIsWUFBWSxFQUFFLFlBQVksQ0FBQztDQUM1QjtBQUVEOzs7Ozs7Ozs7Ozs7Ozs7Ozs7R0FrQkc7QUFDSCxxQkFBYSxpQkFBaUI7SUFVMUIsT0FBTyxDQUFDLFFBQVEsQ0FBQyxNQUFNO0lBVHpCLE9BQU8sQ0FBQyxRQUFRLENBQUMsR0FBRyxDQUFTO0lBQzdCLE9BQU8sQ0FBQyxRQUFRLENBQUMsa0JBQWtCLENBQTRCO0lBQy9ELE9BQU8sQ0FBQyxRQUFRLENBQUMsYUFBYSxDQUFhO0lBRTNDLE9BQU8sQ0FBQyxRQUFRLENBQUMsWUFBWSxDQUFlO0lBQzVDLE9BQU8sQ0FBQyxRQUFRLENBQUMsT0FBTyxDQUFrQjtJQUUxQyxZQUNFLEVBQUUsRUFBRSwwQkFBMEIsRUFDYixNQUFNLEVBQUUsZ0JBQWdCLEVBQ3pDLElBQUksRUFBRSxxQkFBcUIsRUFtQjVCO0lBRUQ7Ozs7Ozs7Ozs7Ozs7Ozs7T0FnQkc7SUFDRyxrQkFBa0IsQ0FDdEIsZ0JBQWdCLEVBQUUsVUFBVSxFQUM1QixXQUFXLEVBQUUsUUFBUSxFQUNyQixPQUFPLEVBQUUseUJBQXlCLEVBQ2xDLE1BQU0sRUFBRSxDQUFDLFdBQVcsRUFBRSxRQUFRLEtBQUssT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUNwRCxPQUFPLENBQUMsU0FBUyxDQUFDLENBdURwQjtJQUVEOztPQUVHO0lBQ0gsSUFBSSxNQUFNLElBQUksTUFBTSxDQUVuQjtJQUVEOzs7T0FHRztJQUNHLEtBQUssa0JBRVY7SUFFRDs7O09BR0c7SUFDRyxJQUFJLGtCQUdUO0NBQ0YifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator_ha_signer.d.ts","sourceRoot":"","sources":["../src/validator_ha_signer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"validator_ha_signer.d.ts","sourceRoot":"","sources":["../src/validator_ha_signer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAEjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EACL,KAAK,gBAAgB,EAErB,KAAK,yBAAyB,EAE/B,MAAM,0BAA0B,CAAC;AAGlC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAE7D,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,eAAe,CAAC;IACzB,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,iBAAiB;IAU1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IATzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA4B;IAC/D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAa;IAE3C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;IAE1C,YACE,EAAE,EAAE,0BAA0B,EACb,MAAM,EAAE,gBAAgB,EACzC,IAAI,EAAE,qBAAqB,EAmB5B;IAED;;;;;;;;;;;;;;;;OAgBG;IACG,kBAAkB,CACtB,gBAAgB,EAAE,UAAU,EAC5B,WAAW,EAAE,QAAQ,EACrB,OAAO,EAAE,yBAAyB,EAClC,MAAM,EAAE,CAAC,WAAW,EAAE,QAAQ,KAAK,OAAO,CAAC,SAAS,CAAC,GACpD,OAAO,CAAC,SAAS,CAAC,CAuDpB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;OAGG;IACG,KAAK,kBAEV;IAED;;;OAGG;IACG,IAAI,kBAGT;CACF"}
|
|
@@ -5,9 +5,8 @@
|
|
|
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 } from '
|
|
8
|
+
import { DutyType, getBlockNumberFromSigningContext } from '@aztec/stdlib/ha-signing';
|
|
9
9
|
import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
10
|
-
import { getBlockNumberFromSigningContext } from './types.js';
|
|
11
10
|
/**
|
|
12
11
|
* Validator High Availability Signer
|
|
13
12
|
*
|
|
@@ -31,18 +30,21 @@ import { getBlockNumberFromSigningContext } from './types.js';
|
|
|
31
30
|
log;
|
|
32
31
|
slashingProtection;
|
|
33
32
|
rollupAddress;
|
|
34
|
-
|
|
33
|
+
dateProvider;
|
|
34
|
+
metrics;
|
|
35
|
+
constructor(db, config, deps){
|
|
35
36
|
this.config = config;
|
|
36
37
|
this.log = createLogger('validator-ha-signer');
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
throw new Error('Validator HA Signer is not enabled in config');
|
|
40
|
-
}
|
|
38
|
+
this.metrics = deps.metrics;
|
|
39
|
+
this.dateProvider = deps.dateProvider;
|
|
41
40
|
if (!config.nodeId || config.nodeId === '') {
|
|
42
41
|
throw new Error('NODE_ID is required for high-availability setups');
|
|
43
42
|
}
|
|
44
43
|
this.rollupAddress = config.l1Contracts.rollupAddress;
|
|
45
|
-
this.slashingProtection = new SlashingProtectionService(db, config
|
|
44
|
+
this.slashingProtection = new SlashingProtectionService(db, config, {
|
|
45
|
+
metrics: deps.metrics,
|
|
46
|
+
dateProvider: deps.dateProvider
|
|
47
|
+
});
|
|
46
48
|
this.log.info('Validator HA Signer initialized with slashing protection', {
|
|
47
49
|
nodeId: config.nodeId,
|
|
48
50
|
rollupAddress: this.rollupAddress.toString()
|
|
@@ -65,6 +67,8 @@ import { getBlockNumberFromSigningContext } from './types.js';
|
|
|
65
67
|
* @throws DutyAlreadySignedError if the duty was already signed (expected in HA)
|
|
66
68
|
* @throws SlashingProtectionError if attempting to sign different data for same slot (expected in HA)
|
|
67
69
|
*/ async signWithProtection(validatorAddress, messageHash, context, signFn) {
|
|
70
|
+
const startTime = this.dateProvider.now();
|
|
71
|
+
const dutyType = context.dutyType;
|
|
68
72
|
let dutyIdentifier;
|
|
69
73
|
if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
70
74
|
dutyIdentifier = {
|
|
@@ -83,6 +87,7 @@ import { getBlockNumberFromSigningContext } from './types.js';
|
|
|
83
87
|
};
|
|
84
88
|
}
|
|
85
89
|
// Acquire lock and get the token for ownership verification
|
|
90
|
+
// DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
|
|
86
91
|
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
87
92
|
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
88
93
|
...dutyIdentifier,
|
|
@@ -100,6 +105,7 @@ import { getBlockNumberFromSigningContext } from './types.js';
|
|
|
100
105
|
...dutyIdentifier,
|
|
101
106
|
lockToken
|
|
102
107
|
});
|
|
108
|
+
this.metrics.recordSigningError(dutyType);
|
|
103
109
|
throw error;
|
|
104
110
|
}
|
|
105
111
|
// Record success (only succeeds if we own the lock)
|
|
@@ -109,6 +115,8 @@ import { getBlockNumberFromSigningContext } from './types.js';
|
|
|
109
115
|
nodeId: this.config.nodeId,
|
|
110
116
|
lockToken
|
|
111
117
|
});
|
|
118
|
+
const duration = this.dateProvider.now() - startTime;
|
|
119
|
+
this.metrics.recordSigningSuccess(dutyType, duration);
|
|
112
120
|
return signature;
|
|
113
121
|
}
|
|
114
122
|
/**
|
package/package.json
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-ha-signer",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.7ffbba4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
|
-
"./config": "./dest/config.js",
|
|
7
6
|
"./db": "./dest/db/index.js",
|
|
8
7
|
"./errors": "./dest/errors.js",
|
|
9
8
|
"./factory": "./dest/factory.js",
|
|
9
|
+
"./metrics": "./dest/metrics.js",
|
|
10
10
|
"./migrations": "./dest/migrations.js",
|
|
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": [
|
|
18
|
-
"./src/config.ts",
|
|
19
19
|
"./src/db/index.ts",
|
|
20
20
|
"./src/errors.ts",
|
|
21
21
|
"./src/factory.ts",
|
|
22
|
+
"./src/metrics.ts",
|
|
22
23
|
"./src/migrations.ts",
|
|
23
24
|
"./src/slashing_protection_service.ts",
|
|
24
25
|
"./src/types.ts",
|
|
@@ -74,8 +75,11 @@
|
|
|
74
75
|
]
|
|
75
76
|
},
|
|
76
77
|
"dependencies": {
|
|
77
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
78
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
78
|
+
"@aztec/ethereum": "0.0.1-commit.7ffbba4",
|
|
79
|
+
"@aztec/foundation": "0.0.1-commit.7ffbba4",
|
|
80
|
+
"@aztec/kv-store": "0.0.1-commit.7ffbba4",
|
|
81
|
+
"@aztec/stdlib": "0.0.1-commit.7ffbba4",
|
|
82
|
+
"@aztec/telemetry-client": "0.0.1-commit.7ffbba4",
|
|
79
83
|
"node-pg-migrate": "^8.0.4",
|
|
80
84
|
"pg": "^8.11.3",
|
|
81
85
|
"tslib": "^2.4.0",
|
package/src/db/index.ts
CHANGED
package/src/db/lmdb.ts
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LMDB implementation of SlashingProtectionDatabase
|
|
3
|
+
*
|
|
4
|
+
* Provides local (single-node) double-signing protection using LMDB as the backend.
|
|
5
|
+
* Suitable for nodes that do NOT run in a high-availability multi-node setup.
|
|
6
|
+
*
|
|
7
|
+
* The LMDB store is single-writer, making setIfNotExists inherently atomic.
|
|
8
|
+
* This means we get crash-restart protection without needing an external database.
|
|
9
|
+
*/
|
|
10
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
11
|
+
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
12
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
13
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
14
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
15
|
+
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
16
|
+
|
|
17
|
+
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
18
|
+
import {
|
|
19
|
+
type CheckAndRecordParams,
|
|
20
|
+
DutyStatus,
|
|
21
|
+
DutyType,
|
|
22
|
+
type StoredDutyRecord,
|
|
23
|
+
getBlockIndexFromDutyIdentifier,
|
|
24
|
+
recordFromFields,
|
|
25
|
+
} from './types.js';
|
|
26
|
+
|
|
27
|
+
function dutyKey(
|
|
28
|
+
rollupAddress: string,
|
|
29
|
+
validatorAddress: string,
|
|
30
|
+
slot: string,
|
|
31
|
+
dutyType: string,
|
|
32
|
+
blockIndexWithinCheckpoint: number,
|
|
33
|
+
): string {
|
|
34
|
+
return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* LMDB-backed implementation of SlashingProtectionDatabase.
|
|
39
|
+
*
|
|
40
|
+
* Provides single-node double-signing protection that survives crashes and restarts.
|
|
41
|
+
* Does not provide cross-node coordination (that requires the PostgreSQL implementation).
|
|
42
|
+
*/
|
|
43
|
+
export class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
44
|
+
public static readonly SCHEMA_VERSION = 1;
|
|
45
|
+
|
|
46
|
+
private readonly duties: AztecAsyncMap<string, StoredDutyRecord>;
|
|
47
|
+
private readonly log: Logger;
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
private readonly store: AztecAsyncKVStore,
|
|
51
|
+
private readonly dateProvider: DateProvider,
|
|
52
|
+
) {
|
|
53
|
+
this.log = createLogger('slashing-protection:lmdb');
|
|
54
|
+
this.duties = store.openMap<string, StoredDutyRecord>('signing-protection-duties');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
59
|
+
*
|
|
60
|
+
* LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
|
|
61
|
+
*/
|
|
62
|
+
public async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
|
|
63
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
64
|
+
const key = dutyKey(
|
|
65
|
+
params.rollupAddress.toString(),
|
|
66
|
+
params.validatorAddress.toString(),
|
|
67
|
+
params.slot.toString(),
|
|
68
|
+
params.dutyType,
|
|
69
|
+
blockIndexWithinCheckpoint,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const lockToken = randomBytes(16).toString('hex');
|
|
73
|
+
const now = this.dateProvider.now();
|
|
74
|
+
|
|
75
|
+
const result = await this.store.transactionAsync(async () => {
|
|
76
|
+
const existing = await this.duties.getAsync(key);
|
|
77
|
+
if (existing) {
|
|
78
|
+
return { isNew: false as const, record: { ...existing, lockToken: '' } };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const newRecord: StoredDutyRecord = {
|
|
82
|
+
rollupAddress: params.rollupAddress.toString(),
|
|
83
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
84
|
+
slot: params.slot.toString(),
|
|
85
|
+
blockNumber: params.blockNumber.toString(),
|
|
86
|
+
blockIndexWithinCheckpoint,
|
|
87
|
+
dutyType: params.dutyType,
|
|
88
|
+
status: DutyStatus.SIGNING,
|
|
89
|
+
messageHash: params.messageHash,
|
|
90
|
+
nodeId: params.nodeId,
|
|
91
|
+
lockToken,
|
|
92
|
+
startedAtMs: now,
|
|
93
|
+
};
|
|
94
|
+
await this.duties.set(key, newRecord);
|
|
95
|
+
return { isNew: true as const, record: newRecord };
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (result.isNew) {
|
|
99
|
+
this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
|
|
100
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
101
|
+
nodeId: params.nodeId,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { isNew: result.isNew, record: recordFromFields(result.record) };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Update a duty to 'signed' status with the signature.
|
|
110
|
+
* Only succeeds if the lockToken matches.
|
|
111
|
+
*/
|
|
112
|
+
public updateDutySigned(
|
|
113
|
+
rollupAddress: EthAddress,
|
|
114
|
+
validatorAddress: EthAddress,
|
|
115
|
+
slot: SlotNumber,
|
|
116
|
+
dutyType: DutyType,
|
|
117
|
+
signature: string,
|
|
118
|
+
lockToken: string,
|
|
119
|
+
blockIndexWithinCheckpoint: number,
|
|
120
|
+
): Promise<boolean> {
|
|
121
|
+
const key = dutyKey(
|
|
122
|
+
rollupAddress.toString(),
|
|
123
|
+
validatorAddress.toString(),
|
|
124
|
+
slot.toString(),
|
|
125
|
+
dutyType,
|
|
126
|
+
blockIndexWithinCheckpoint,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
return this.store.transactionAsync(async () => {
|
|
130
|
+
const existing = await this.duties.getAsync(key);
|
|
131
|
+
if (!existing) {
|
|
132
|
+
this.log.warn('Failed to update duty to signed: duty not found', {
|
|
133
|
+
rollupAddress: rollupAddress.toString(),
|
|
134
|
+
validatorAddress: validatorAddress.toString(),
|
|
135
|
+
slot: slot.toString(),
|
|
136
|
+
dutyType,
|
|
137
|
+
blockIndexWithinCheckpoint,
|
|
138
|
+
});
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (existing.lockToken !== lockToken) {
|
|
143
|
+
this.log.warn('Failed to update duty to signed: invalid token', {
|
|
144
|
+
rollupAddress: rollupAddress.toString(),
|
|
145
|
+
validatorAddress: validatorAddress.toString(),
|
|
146
|
+
slot: slot.toString(),
|
|
147
|
+
dutyType,
|
|
148
|
+
blockIndexWithinCheckpoint,
|
|
149
|
+
});
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
await this.duties.set(key, {
|
|
154
|
+
...existing,
|
|
155
|
+
status: DutyStatus.SIGNED,
|
|
156
|
+
signature,
|
|
157
|
+
completedAtMs: this.dateProvider.now(),
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return true;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Delete a duty record.
|
|
166
|
+
* Only succeeds if the lockToken matches.
|
|
167
|
+
*/
|
|
168
|
+
public deleteDuty(
|
|
169
|
+
rollupAddress: EthAddress,
|
|
170
|
+
validatorAddress: EthAddress,
|
|
171
|
+
slot: SlotNumber,
|
|
172
|
+
dutyType: DutyType,
|
|
173
|
+
lockToken: string,
|
|
174
|
+
blockIndexWithinCheckpoint: number,
|
|
175
|
+
): Promise<boolean> {
|
|
176
|
+
const key = dutyKey(
|
|
177
|
+
rollupAddress.toString(),
|
|
178
|
+
validatorAddress.toString(),
|
|
179
|
+
slot.toString(),
|
|
180
|
+
dutyType,
|
|
181
|
+
blockIndexWithinCheckpoint,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
return this.store.transactionAsync(async () => {
|
|
185
|
+
const existing = await this.duties.getAsync(key);
|
|
186
|
+
if (!existing || existing.lockToken !== lockToken) {
|
|
187
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
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.delete(key);
|
|
198
|
+
return true;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
204
|
+
*/
|
|
205
|
+
public cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
206
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
207
|
+
|
|
208
|
+
return this.store.transactionAsync(async () => {
|
|
209
|
+
const keysToDelete: string[] = [];
|
|
210
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
211
|
+
if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
|
|
212
|
+
keysToDelete.push(key);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
for (const key of keysToDelete) {
|
|
216
|
+
await this.duties.delete(key);
|
|
217
|
+
}
|
|
218
|
+
return keysToDelete.length;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Cleanup duties with outdated rollup address.
|
|
224
|
+
*
|
|
225
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
226
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
227
|
+
* startup whenever the rollup address changes.
|
|
228
|
+
*/
|
|
229
|
+
public cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
|
|
230
|
+
return Promise.resolve(0);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
235
|
+
*/
|
|
236
|
+
public cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
237
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
238
|
+
|
|
239
|
+
return this.store.transactionAsync(async () => {
|
|
240
|
+
const keysToDelete: string[] = [];
|
|
241
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
242
|
+
if (
|
|
243
|
+
record.status === DutyStatus.SIGNED &&
|
|
244
|
+
record.completedAtMs !== undefined &&
|
|
245
|
+
record.completedAtMs < cutoffMs
|
|
246
|
+
) {
|
|
247
|
+
keysToDelete.push(key);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
for (const key of keysToDelete) {
|
|
251
|
+
await this.duties.delete(key);
|
|
252
|
+
}
|
|
253
|
+
return keysToDelete.length;
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Close the underlying LMDB store.
|
|
259
|
+
*/
|
|
260
|
+
public async close(): Promise<void> {
|
|
261
|
+
await this.store.close();
|
|
262
|
+
this.log.debug('LMDB slashing protection database closed');
|
|
263
|
+
}
|
|
264
|
+
}
|
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.
|
|
@@ -220,14 +220,16 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
/**
|
|
223
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
223
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
224
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
225
|
+
* then delegates to the shared recordFromFields() converter.
|
|
224
226
|
*/
|
|
225
227
|
private rowToRecord(row: DutyRow): ValidatorDutyRecord {
|
|
226
|
-
return {
|
|
227
|
-
rollupAddress:
|
|
228
|
-
validatorAddress:
|
|
229
|
-
slot:
|
|
230
|
-
blockNumber:
|
|
228
|
+
return recordFromFields({
|
|
229
|
+
rollupAddress: row.rollup_address,
|
|
230
|
+
validatorAddress: row.validator_address,
|
|
231
|
+
slot: row.slot,
|
|
232
|
+
blockNumber: row.block_number,
|
|
231
233
|
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
232
234
|
dutyType: row.duty_type,
|
|
233
235
|
status: row.status,
|
|
@@ -235,10 +237,10 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
235
237
|
signature: row.signature ?? undefined,
|
|
236
238
|
nodeId: row.node_id,
|
|
237
239
|
lockToken: row.lock_token,
|
|
238
|
-
|
|
239
|
-
|
|
240
|
+
startedAtMs: row.started_at.getTime(),
|
|
241
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
240
242
|
errorMessage: row.error_message ?? undefined,
|
|
241
|
-
};
|
|
243
|
+
});
|
|
242
244
|
}
|
|
243
245
|
|
|
244
246
|
/**
|
|
@@ -254,8 +256,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
254
256
|
* @returns the number of duties cleaned up
|
|
255
257
|
*/
|
|
256
258
|
async cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
257
|
-
const
|
|
258
|
-
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, cutoff]);
|
|
259
|
+
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, maxAgeMs]);
|
|
259
260
|
return result.rowCount ?? 0;
|
|
260
261
|
}
|
|
261
262
|
|
|
@@ -277,8 +278,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
|
|
|
277
278
|
* @returns the number of duties cleaned up
|
|
278
279
|
*/
|
|
279
280
|
async cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
280
|
-
const
|
|
281
|
-
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [cutoff]);
|
|
281
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [maxAgeMs]);
|
|
282
282
|
return result.rowCount ?? 0;
|
|
283
283
|
}
|
|
284
284
|
}
|
package/src/db/schema.ts
CHANGED
|
@@ -203,23 +203,24 @@ WHERE status = 'signed'
|
|
|
203
203
|
|
|
204
204
|
/**
|
|
205
205
|
* Query to clean up old duties (for maintenance)
|
|
206
|
-
* Removes SIGNED duties older than a specified
|
|
206
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
207
207
|
*/
|
|
208
208
|
export const CLEANUP_OLD_DUTIES = `
|
|
209
209
|
DELETE FROM validator_duties
|
|
210
210
|
WHERE status = 'signed'
|
|
211
|
-
AND started_at < $1;
|
|
211
|
+
AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
|
|
212
212
|
`;
|
|
213
213
|
|
|
214
214
|
/**
|
|
215
215
|
* Query to cleanup own stuck duties
|
|
216
216
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
217
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
217
218
|
*/
|
|
218
219
|
export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
219
220
|
DELETE FROM validator_duties
|
|
220
221
|
WHERE node_id = $1
|
|
221
222
|
AND status = 'signing'
|
|
222
|
-
AND started_at < $2;
|
|
223
|
+
AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
|
|
223
224
|
`;
|
|
224
225
|
|
|
225
226
|
/**
|
package/src/db/types.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
BlockNumber,
|
|
3
|
+
type CheckpointNumber,
|
|
4
|
+
type IndexWithinCheckpoint,
|
|
5
|
+
SlotNumber,
|
|
6
|
+
} from '@aztec/foundation/branded-types';
|
|
7
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
8
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
9
|
+
import { DutyType } from '@aztec/stdlib/ha-signing';
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
12
|
* Row type from PostgreSQL query
|
|
@@ -23,24 +29,34 @@ export interface DutyRow {
|
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
/**
|
|
26
|
-
*
|
|
32
|
+
* Plain-primitive representation of a duty record suitable for serialization
|
|
33
|
+
* (e.g. msgpackr for LMDB). All domain types are stored as their string/number
|
|
34
|
+
* equivalents. Timestamps are Unix milliseconds.
|
|
27
35
|
*/
|
|
28
|
-
export interface
|
|
29
|
-
|
|
36
|
+
export interface StoredDutyRecord {
|
|
37
|
+
rollupAddress: string;
|
|
38
|
+
validatorAddress: string;
|
|
39
|
+
slot: string;
|
|
40
|
+
blockNumber: string;
|
|
41
|
+
blockIndexWithinCheckpoint: number;
|
|
42
|
+
dutyType: DutyType;
|
|
43
|
+
status: DutyStatus;
|
|
44
|
+
messageHash: string;
|
|
45
|
+
signature?: string;
|
|
46
|
+
nodeId: string;
|
|
47
|
+
lockToken: string;
|
|
48
|
+
/** Unix timestamp in milliseconds when signing started */
|
|
49
|
+
startedAtMs: number;
|
|
50
|
+
/** Unix timestamp in milliseconds when signing completed */
|
|
51
|
+
completedAtMs?: number;
|
|
52
|
+
errorMessage?: string;
|
|
30
53
|
}
|
|
31
54
|
|
|
32
55
|
/**
|
|
33
|
-
*
|
|
56
|
+
* Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
|
|
34
57
|
*/
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
CHECKPOINT_PROPOSAL = 'CHECKPOINT_PROPOSAL',
|
|
38
|
-
ATTESTATION = 'ATTESTATION',
|
|
39
|
-
ATTESTATIONS_AND_SIGNERS = 'ATTESTATIONS_AND_SIGNERS',
|
|
40
|
-
GOVERNANCE_VOTE = 'GOVERNANCE_VOTE',
|
|
41
|
-
SLASHING_VOTE = 'SLASHING_VOTE',
|
|
42
|
-
AUTH_REQUEST = 'AUTH_REQUEST',
|
|
43
|
-
TXS = 'TXS',
|
|
58
|
+
export interface InsertOrGetRow extends DutyRow {
|
|
59
|
+
is_new: boolean;
|
|
44
60
|
}
|
|
45
61
|
|
|
46
62
|
/**
|
|
@@ -51,8 +67,12 @@ export enum DutyStatus {
|
|
|
51
67
|
SIGNED = 'signed',
|
|
52
68
|
}
|
|
53
69
|
|
|
70
|
+
// Re-export DutyType from stdlib
|
|
71
|
+
export { DutyType };
|
|
72
|
+
|
|
54
73
|
/**
|
|
55
|
-
*
|
|
74
|
+
* Rich representation of a validator duty, with branded types and Date objects.
|
|
75
|
+
* This is the common output type returned by all SlashingProtectionDatabase implementations.
|
|
56
76
|
*/
|
|
57
77
|
export interface ValidatorDutyRecord {
|
|
58
78
|
/** Ethereum address of the rollup contract */
|
|
@@ -85,6 +105,31 @@ export interface ValidatorDutyRecord {
|
|
|
85
105
|
errorMessage?: string;
|
|
86
106
|
}
|
|
87
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Convert a {@link StoredDutyRecord} (plain-primitive wire format) to a
|
|
110
|
+
* {@link ValidatorDutyRecord} (rich domain type).
|
|
111
|
+
*
|
|
112
|
+
* Shared by LMDB and any future non-Postgres backend implementations.
|
|
113
|
+
*/
|
|
114
|
+
export function recordFromFields(stored: StoredDutyRecord): ValidatorDutyRecord {
|
|
115
|
+
return {
|
|
116
|
+
rollupAddress: EthAddress.fromString(stored.rollupAddress),
|
|
117
|
+
validatorAddress: EthAddress.fromString(stored.validatorAddress),
|
|
118
|
+
slot: SlotNumber.fromString(stored.slot),
|
|
119
|
+
blockNumber: BlockNumber.fromString(stored.blockNumber),
|
|
120
|
+
blockIndexWithinCheckpoint: stored.blockIndexWithinCheckpoint,
|
|
121
|
+
dutyType: stored.dutyType,
|
|
122
|
+
status: stored.status,
|
|
123
|
+
messageHash: stored.messageHash,
|
|
124
|
+
signature: stored.signature,
|
|
125
|
+
nodeId: stored.nodeId,
|
|
126
|
+
lockToken: stored.lockToken,
|
|
127
|
+
startedAt: new Date(stored.startedAtMs),
|
|
128
|
+
completedAt: stored.completedAtMs !== undefined ? new Date(stored.completedAtMs) : undefined,
|
|
129
|
+
errorMessage: stored.errorMessage,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
88
133
|
/**
|
|
89
134
|
* Duty identifier for block proposals.
|
|
90
135
|
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|