@aztec/validator-ha-signer 0.0.1-commit.f504929 → 0.0.1-commit.f5a9928
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 +2 -4
- package/dest/db/index.d.ts +2 -1
- package/dest/db/index.d.ts.map +1 -1
- package/dest/db/index.js +1 -0
- package/dest/db/lmdb.d.ts +70 -0
- package/dest/db/lmdb.d.ts.map +1 -0
- package/dest/db/lmdb.js +223 -0
- package/dest/db/migrations/1_initial-schema.d.ts +4 -2
- package/dest/db/migrations/1_initial-schema.d.ts.map +1 -1
- package/dest/db/migrations/1_initial-schema.js +34 -4
- package/dest/db/migrations/2_add-checkpoint-number.d.ts +7 -0
- package/dest/db/migrations/2_add-checkpoint-number.d.ts.map +1 -0
- package/dest/db/migrations/2_add-checkpoint-number.js +17 -0
- package/dest/db/postgres.d.ts +4 -2
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +15 -13
- package/dest/db/schema.d.ts +6 -6
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +9 -4
- package/dest/db/types.d.ts +44 -7
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +26 -0
- package/dest/errors.d.ts +14 -1
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +15 -0
- package/dest/factory.d.ts +38 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +95 -9
- package/dest/slashing_protection_service.d.ts +6 -4
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +14 -11
- package/dest/types.d.ts +8 -3
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +2 -1
- package/dest/validator_ha_signer.d.ts +5 -4
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +33 -12
- package/package.json +9 -7
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +308 -0
- package/src/db/migrations/1_initial-schema.ts +35 -4
- package/src/db/migrations/2_add-checkpoint-number.ts +19 -0
- package/src/db/postgres.ts +15 -11
- package/src/db/schema.ts +9 -4
- package/src/db/types.ts +63 -6
- package/src/errors.ts +21 -0
- package/src/factory.ts +130 -7
- package/src/slashing_protection_service.ts +18 -13
- package/src/types.ts +11 -0
- package/src/validator_ha_signer.ts +48 -13
|
@@ -8,6 +8,7 @@ import { RunningPromise } from '@aztec/foundation/promise';
|
|
|
8
8
|
import { sleep } from '@aztec/foundation/sleep';
|
|
9
9
|
import { DutyStatus, getBlockIndexFromDutyIdentifier } from './db/types.js';
|
|
10
10
|
import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
11
|
+
/** Default max age (ms) of a stuck SIGNING duty before cleanup reclaims it: 2x the 72s Aztec slot duration. */ export const DEFAULT_MAX_STUCK_DUTIES_AGE_MS = 144_000;
|
|
11
12
|
/**
|
|
12
13
|
* Slashing Protection Service
|
|
13
14
|
*
|
|
@@ -27,7 +28,7 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
27
28
|
config;
|
|
28
29
|
log;
|
|
29
30
|
pollingIntervalMs;
|
|
30
|
-
|
|
31
|
+
peerSigningTimeoutMs;
|
|
31
32
|
maxStuckDutiesAgeMs;
|
|
32
33
|
metrics;
|
|
33
34
|
dateProvider;
|
|
@@ -38,9 +39,8 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
38
39
|
this.config = config;
|
|
39
40
|
this.log = createLogger('slashing-protection');
|
|
40
41
|
this.pollingIntervalMs = config.pollingIntervalMs;
|
|
41
|
-
this.
|
|
42
|
-
|
|
43
|
-
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
42
|
+
this.peerSigningTimeoutMs = config.peerSigningTimeoutMs;
|
|
43
|
+
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? DEFAULT_MAX_STUCK_DUTIES_AGE_MS;
|
|
44
44
|
this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
|
|
45
45
|
this.metrics = deps.metrics;
|
|
46
46
|
this.dateProvider = deps.dateProvider;
|
|
@@ -71,7 +71,7 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
71
71
|
const { isNew, record } = await this.db.tryInsertOrGetExisting(params);
|
|
72
72
|
if (isNew) {
|
|
73
73
|
// We successfully acquired the lock
|
|
74
|
-
this.log.
|
|
74
|
+
this.log.verbose(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
|
|
75
75
|
validatorAddress: validatorAddress.toString(),
|
|
76
76
|
nodeId
|
|
77
77
|
});
|
|
@@ -96,10 +96,10 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
96
96
|
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
|
|
97
97
|
} else if (record.status === DutyStatus.SIGNING) {
|
|
98
98
|
// Another node is currently signing - check for timeout
|
|
99
|
-
if (this.dateProvider.now() - startTime > this.
|
|
99
|
+
if (this.dateProvider.now() - startTime > this.peerSigningTimeoutMs) {
|
|
100
100
|
this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
|
|
101
101
|
validatorAddress: validatorAddress.toString(),
|
|
102
|
-
timeoutMs: this.
|
|
102
|
+
timeoutMs: this.peerSigningTimeoutMs,
|
|
103
103
|
signingNodeId: record.nodeId
|
|
104
104
|
});
|
|
105
105
|
this.metrics.recordDutyAlreadySigned(dutyType);
|
|
@@ -128,7 +128,7 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
128
128
|
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
129
129
|
const success = await this.db.updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature.toString(), lockToken, blockIndexWithinCheckpoint);
|
|
130
130
|
if (success) {
|
|
131
|
-
this.log.
|
|
131
|
+
this.log.verbose(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
|
|
132
132
|
validatorAddress: validatorAddress.toString(),
|
|
133
133
|
nodeId
|
|
134
134
|
});
|
|
@@ -174,10 +174,10 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
174
174
|
* Also performs one-time cleanup of duties with outdated rollup addresses.
|
|
175
175
|
*/ async start() {
|
|
176
176
|
// One-time cleanup at startup: remove duties from previous rollup versions
|
|
177
|
-
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.
|
|
177
|
+
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.rollupAddress);
|
|
178
178
|
if (numOutdatedRollupDuties > 0) {
|
|
179
179
|
this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
|
|
180
|
-
currentRollupAddress: this.config.
|
|
180
|
+
currentRollupAddress: this.config.rollupAddress.toString()
|
|
181
181
|
});
|
|
182
182
|
this.metrics.recordCleanup('outdated_rollup', numOutdatedRollupDuties);
|
|
183
183
|
}
|
|
@@ -205,7 +205,10 @@ import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
|
205
205
|
* Periodic cleanup of stuck duties and optionally old signed duties.
|
|
206
206
|
* Runs in the background via RunningPromise.
|
|
207
207
|
*/ async cleanup() {
|
|
208
|
-
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status)
|
|
208
|
+
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status).
|
|
209
|
+
// This cannot race an in-flight signing: every signing operation is hard-bounded by a timeout
|
|
210
|
+
// clamped below maxStuckDutiesAgeMs / 2 (see ValidatorHASigner), so a live SIGNING row is
|
|
211
|
+
// always released long before it can be considered stuck.
|
|
209
212
|
const numStuckDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
|
|
210
213
|
if (numStuckDuties > 0) {
|
|
211
214
|
this.log.verbose(`Cleaned up ${numStuckDuties} stuck duties`, {
|
package/dest/types.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
2
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
3
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
4
|
-
import { DutyType, type HAProtectedSigningContext, type SigningContext, type ValidatorHASignerConfig, getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib, isHAProtectedContext } from '@aztec/stdlib/ha-signing';
|
|
4
|
+
import { type AttestationSigningContext, type CheckpointProposalSigningContext, DutyType, type HAProtectedSigningContext, type SigningContext, type ValidatorHASignerConfig, getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib, getCheckpointNumberFromSigningContext as getCheckpointNumberFromSigningContextFromStdlib, isHAProtectedContext } from '@aztec/stdlib/ha-signing';
|
|
5
5
|
import type { TelemetryClient } from '@aztec/telemetry-client';
|
|
6
6
|
import type { Pool } from 'pg';
|
|
7
7
|
import type { BlockProposalDutyIdentifier, CheckAndRecordParams, DeleteDutyParams, DutyIdentifier, DutyRow, OtherDutyIdentifier, RecordSuccessParams, ValidatorDutyRecord } from './db/types.js';
|
|
8
|
-
export type { BlockProposalDutyIdentifier, CheckAndRecordParams, DeleteDutyParams, DutyIdentifier, DutyRow, HAProtectedSigningContext, OtherDutyIdentifier, RecordSuccessParams, SigningContext, ValidatorDutyRecord, ValidatorHASignerConfig, };
|
|
8
|
+
export type { AttestationSigningContext, BlockProposalDutyIdentifier, CheckAndRecordParams, CheckpointProposalSigningContext, DeleteDutyParams, DutyIdentifier, DutyRow, HAProtectedSigningContext, OtherDutyIdentifier, RecordSuccessParams, SigningContext, ValidatorDutyRecord, ValidatorHASignerConfig, };
|
|
9
9
|
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
10
10
|
export { isHAProtectedContext };
|
|
11
11
|
export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
|
|
12
|
+
export { getCheckpointNumberFromSigningContextFromStdlib as getCheckpointNumberFromSigningContext };
|
|
12
13
|
/**
|
|
13
14
|
* Result of tryInsertOrGetExisting operation
|
|
14
15
|
*/
|
|
@@ -36,6 +37,10 @@ export interface CreateHASignerDeps {
|
|
|
36
37
|
*/
|
|
37
38
|
dateProvider?: DateProvider;
|
|
38
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* deps for creating a local signing protection signer
|
|
42
|
+
*/
|
|
43
|
+
export type CreateLocalSignerWithProtectionDeps = Omit<CreateHASignerDeps, 'pool'>;
|
|
39
44
|
/**
|
|
40
45
|
* Database interface for slashing protection operations
|
|
41
46
|
* This abstraction allows for different database implementations (PostgreSQL, SQLite, etc.)
|
|
@@ -92,4 +97,4 @@ export interface SlashingProtectionDatabase {
|
|
|
92
97
|
*/
|
|
93
98
|
close(): Promise<void>;
|
|
94
99
|
}
|
|
95
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
100
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy90eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFDN0QsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDaEUsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQ3ZELE9BQU8sRUFDTCxLQUFLLHlCQUF5QixFQUM5QixLQUFLLGdDQUFnQyxFQUNyQyxRQUFRLEVBQ1IsS0FBSyx5QkFBeUIsRUFDOUIsS0FBSyxjQUFjLEVBQ25CLEtBQUssdUJBQXVCLEVBQzVCLGdDQUFnQyxJQUFJLDBDQUEwQyxFQUM5RSxxQ0F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OAGG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEzE;;;;;OAKG;IACH,2BAA2B,CAAC,oBAAoB,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE/E;;;;OAIG;IACH,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEpD;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
|
package/dest/types.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib, isHAProtectedContext } from '@aztec/stdlib/ha-signing';
|
|
1
|
+
import { getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib, getCheckpointNumberFromSigningContext as getCheckpointNumberFromSigningContextFromStdlib, isHAProtectedContext } from '@aztec/stdlib/ha-signing';
|
|
2
2
|
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
3
3
|
export { isHAProtectedContext };
|
|
4
4
|
export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
|
|
5
|
+
export { getCheckpointNumberFromSigningContextFromStdlib as getCheckpointNumberFromSigningContext };
|
|
@@ -8,8 +8,8 @@
|
|
|
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
13
|
import type { HASignerMetrics } from './metrics.js';
|
|
14
14
|
import type { SlashingProtectionDatabase } from './types.js';
|
|
15
15
|
export interface ValidatorHASignerDeps {
|
|
@@ -42,7 +42,8 @@ export declare class ValidatorHASigner {
|
|
|
42
42
|
private readonly rollupAddress;
|
|
43
43
|
private readonly dateProvider;
|
|
44
44
|
private readonly metrics;
|
|
45
|
-
|
|
45
|
+
private readonly signerCallTimeoutMs;
|
|
46
|
+
constructor(db: SlashingProtectionDatabase, config: BaseSignerConfig, deps: ValidatorHASignerDeps);
|
|
46
47
|
/**
|
|
47
48
|
* Sign a message with slashing protection.
|
|
48
49
|
*
|
|
@@ -76,4 +77,4 @@ export declare class ValidatorHASigner {
|
|
|
76
77
|
*/
|
|
77
78
|
stop(): Promise<void>;
|
|
78
79
|
}
|
|
79
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
80
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdG9yX2hhX3NpZ25lci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3ZhbGlkYXRvcl9oYV9zaWduZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBQ0gsT0FBTyxLQUFLLEVBQUUsUUFBUSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDekQsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBQzNELE9BQU8sS0FBSyxFQUFFLFNBQVMsRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBRWpFLE9BQU8sRUFBRSxLQUFLLFlBQVksRUFBa0IsTUFBTSx5QkFBeUIsQ0FBQztBQUM1RSxPQUFPLEVBQ0wsS0FBSyxnQkFBZ0IsRUFFckIsS0FBSyx5QkFBeUIsRUFHL0IsTUFBTSwwQkFBMEIsQ0FBQztBQUlsQyxPQUFPLEtBQUssRUFBRSxlQUFlLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFFcEQsT0FBTyxLQUFLLEVBQ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7SUFDRyxJQUFJLGtCQUdUO0NBQ0YifQ==
|
|
@@ -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;AAEjE,OAAO,KAAK,
|
|
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,EAAE,KAAK,YAAY,EAAkB,MAAM,yBAAyB,CAAC;AAC5E,OAAO,EACL,KAAK,gBAAgB,EAErB,KAAK,yBAAyB,EAG/B,MAAM,0BAA0B,CAAC;AAIlC,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;AAKD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,iBAAiB;IAW1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAVzB,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;IAC1C,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAS;IAE7C,YACE,EAAE,EAAE,0BAA0B,EACb,MAAM,EAAE,gBAAgB,EACzC,IAAI,EAAE,qBAAqB,EAiC5B;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,CA2EpB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;OAGG;IACG,KAAK,kBAEV;IAED;;;OAGG;IACG,IAAI,kBAGT;CACF"}
|
|
@@ -5,8 +5,11 @@
|
|
|
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 {
|
|
9
|
-
import {
|
|
8
|
+
import { executeTimeout } from '@aztec/foundation/timer';
|
|
9
|
+
import { DutyType, getBlockNumberFromSigningContext, getCheckpointNumberFromSigningContext } from '@aztec/stdlib/ha-signing';
|
|
10
|
+
import { SigningLockLostError } from './errors.js';
|
|
11
|
+
import { DEFAULT_MAX_STUCK_DUTIES_AGE_MS, SlashingProtectionService } from './slashing_protection_service.js';
|
|
12
|
+
/** Default hard timeout (ms) for a single signer call when not configured. */ const DEFAULT_SIGNER_CALL_TIMEOUT_MS = 30_000;
|
|
10
13
|
/**
|
|
11
14
|
* Validator High Availability Signer
|
|
12
15
|
*
|
|
@@ -32,26 +35,32 @@ import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
|
32
35
|
rollupAddress;
|
|
33
36
|
dateProvider;
|
|
34
37
|
metrics;
|
|
38
|
+
signerCallTimeoutMs;
|
|
35
39
|
constructor(db, config, deps){
|
|
36
40
|
this.config = config;
|
|
37
41
|
this.log = createLogger('validator-ha-signer');
|
|
38
42
|
this.metrics = deps.metrics;
|
|
39
43
|
this.dateProvider = deps.dateProvider;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
// Clamp the signer-call timeout below half the stuck-duty max age. This maintains the
|
|
45
|
+
// invariant that an in-flight signing always times out and releases its SIGNING row well before
|
|
46
|
+
// stuck-duty cleanup could consider it stuck, so cleanup can never delete a live duty (only
|
|
47
|
+
// signWithProtection writes SIGNING rows, and every path through it is bounded by this timeout).
|
|
48
|
+
// If timers misbehave anyway, the recordSuccess-returns-false throw is the backstop: the duty
|
|
49
|
+
// fails instead of broadcasting an unprotected signature.
|
|
50
|
+
const maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? DEFAULT_MAX_STUCK_DUTIES_AGE_MS;
|
|
51
|
+
this.signerCallTimeoutMs = Math.min(config.signerCallTimeoutMs ?? DEFAULT_SIGNER_CALL_TIMEOUT_MS, maxStuckDutiesAgeMs / 2);
|
|
44
52
|
if (!config.nodeId || config.nodeId === '') {
|
|
45
53
|
throw new Error('NODE_ID is required for high-availability setups');
|
|
46
54
|
}
|
|
47
|
-
this.rollupAddress = config.
|
|
55
|
+
this.rollupAddress = config.rollupAddress;
|
|
48
56
|
this.slashingProtection = new SlashingProtectionService(db, config, {
|
|
49
57
|
metrics: deps.metrics,
|
|
50
58
|
dateProvider: deps.dateProvider
|
|
51
59
|
});
|
|
52
60
|
this.log.info('Validator HA Signer initialized with slashing protection', {
|
|
53
61
|
nodeId: config.nodeId,
|
|
54
|
-
rollupAddress: this.rollupAddress.toString()
|
|
62
|
+
rollupAddress: this.rollupAddress.toString(),
|
|
63
|
+
signerCallTimeoutMs: this.signerCallTimeoutMs
|
|
55
64
|
});
|
|
56
65
|
}
|
|
57
66
|
/**
|
|
@@ -93,16 +102,20 @@ import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
|
93
102
|
// Acquire lock and get the token for ownership verification
|
|
94
103
|
// DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
|
|
95
104
|
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
105
|
+
const checkpointNumber = getCheckpointNumberFromSigningContext(context);
|
|
96
106
|
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
97
107
|
...dutyIdentifier,
|
|
98
108
|
blockNumber,
|
|
109
|
+
checkpointNumber,
|
|
99
110
|
messageHash: messageHash.toString(),
|
|
100
111
|
nodeId: this.config.nodeId
|
|
101
112
|
});
|
|
102
|
-
// Perform signing
|
|
113
|
+
// Perform signing under a hard timeout. If the signer hangs, executeTimeout aborts and rejects;
|
|
114
|
+
// the orphaned signFn promise resolving later is discarded (never broadcast). A timeout takes the
|
|
115
|
+
// same failure path as any signing error: release the lock so the duty can be retried safely.
|
|
103
116
|
let signature;
|
|
104
117
|
try {
|
|
105
|
-
signature = await signFn(messageHash);
|
|
118
|
+
signature = await executeTimeout(()=>signFn(messageHash), this.signerCallTimeoutMs, ()=>new Error(`Signing operation for ${dutyType} at slot ${context.slot} timed out after ` + `${this.signerCallTimeoutMs}ms`));
|
|
106
119
|
} catch (error) {
|
|
107
120
|
// Delete duty to allow retry (only succeeds if we own the lock)
|
|
108
121
|
await this.slashingProtection.deleteDuty({
|
|
@@ -112,13 +125,21 @@ import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
|
112
125
|
this.metrics.recordSigningError(dutyType);
|
|
113
126
|
throw error;
|
|
114
127
|
}
|
|
115
|
-
// Record success (only succeeds if we own the lock)
|
|
116
|
-
|
|
128
|
+
// Record success (only succeeds if we still own the lock).
|
|
129
|
+
// A false result means our SIGNING row is gone or no longer ours (e.g. deleted by stuck-duty
|
|
130
|
+
// cleanup while signing was slow). We must not broadcast this signature: without a protection
|
|
131
|
+
// record, a later attempt for the same duty with different data would sign freely (slashable).
|
|
132
|
+
// Do not delete the duty here - we no longer own it, and another node may legitimately hold it.
|
|
133
|
+
const recorded = await this.slashingProtection.recordSuccess({
|
|
117
134
|
...dutyIdentifier,
|
|
118
135
|
signature,
|
|
119
136
|
nodeId: this.config.nodeId,
|
|
120
137
|
lockToken
|
|
121
138
|
});
|
|
139
|
+
if (!recorded) {
|
|
140
|
+
this.metrics.recordSigningError(dutyType);
|
|
141
|
+
throw new SigningLockLostError(context.slot, dutyType, this.config.nodeId);
|
|
142
|
+
}
|
|
122
143
|
const duration = this.dateProvider.now() - startTime;
|
|
123
144
|
this.metrics.recordSigningSuccess(dutyType, duration);
|
|
124
145
|
return signature;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-ha-signer",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.f5a9928",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./db": "./dest/db/index.js",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"./slashing-protection-service": "./dest/slashing_protection_service.js",
|
|
12
12
|
"./types": "./dest/types.js",
|
|
13
13
|
"./validator-ha-signer": "./dest/validator_ha_signer.js",
|
|
14
|
-
"./test": "./dest/test/pglite_pool.js"
|
|
14
|
+
"./test": "./dest/test/pglite_pool.js",
|
|
15
|
+
"./db/lmdb": "./dest/db/lmdb.js"
|
|
15
16
|
},
|
|
16
17
|
"typedocOptions": {
|
|
17
18
|
"entryPoints": [
|
|
@@ -74,14 +75,15 @@
|
|
|
74
75
|
]
|
|
75
76
|
},
|
|
76
77
|
"dependencies": {
|
|
77
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
78
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
79
|
-
"@aztec/
|
|
80
|
-
"@aztec/
|
|
78
|
+
"@aztec/ethereum": "0.0.1-commit.f5a9928",
|
|
79
|
+
"@aztec/foundation": "0.0.1-commit.f5a9928",
|
|
80
|
+
"@aztec/kv-store": "0.0.1-commit.f5a9928",
|
|
81
|
+
"@aztec/stdlib": "0.0.1-commit.f5a9928",
|
|
82
|
+
"@aztec/telemetry-client": "0.0.1-commit.f5a9928",
|
|
81
83
|
"node-pg-migrate": "^8.0.4",
|
|
82
84
|
"pg": "^8.11.3",
|
|
83
85
|
"tslib": "^2.4.0",
|
|
84
|
-
"zod": "^
|
|
86
|
+
"zod": "^4"
|
|
85
87
|
},
|
|
86
88
|
"devDependencies": {
|
|
87
89
|
"@electric-sql/pglite": "^0.3.14",
|
package/src/db/index.ts
CHANGED
package/src/db/lmdb.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LMDB implementation of SlashingProtectionDatabase
|
|
3
|
+
*
|
|
4
|
+
* Provides local (single-node) double-signing protection using LMDB as the backend.
|
|
5
|
+
* Suitable for nodes that do NOT run in a high-availability multi-node setup.
|
|
6
|
+
*
|
|
7
|
+
* The LMDB store is single-writer, making setIfNotExists inherently atomic.
|
|
8
|
+
* This means we get crash-restart protection without needing an external database.
|
|
9
|
+
*/
|
|
10
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
11
|
+
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
12
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
13
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
14
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
15
|
+
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
16
|
+
import { openStoreAt } from '@aztec/kv-store/lmdb-v2';
|
|
17
|
+
|
|
18
|
+
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
19
|
+
import {
|
|
20
|
+
type CheckAndRecordParams,
|
|
21
|
+
DutyStatus,
|
|
22
|
+
DutyType,
|
|
23
|
+
type StoredDutyRecord,
|
|
24
|
+
getBlockIndexFromDutyIdentifier,
|
|
25
|
+
recordFromFields,
|
|
26
|
+
} from './types.js';
|
|
27
|
+
|
|
28
|
+
const DUTIES_MAP_NAME = 'signing-protection-duties';
|
|
29
|
+
const LEGACY_CHECKPOINT_NUMBER = '0';
|
|
30
|
+
|
|
31
|
+
type StoredDutyRecordV1 = Omit<StoredDutyRecord, 'checkpointNumber'> & { checkpointNumber?: undefined };
|
|
32
|
+
type MigratableStoredDutyRecord = StoredDutyRecord | StoredDutyRecordV1;
|
|
33
|
+
|
|
34
|
+
function needsCheckpointNumberMigration(record: MigratableStoredDutyRecord): record is StoredDutyRecordV1 {
|
|
35
|
+
return record.checkpointNumber === undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Migrates local slashing-protection duties from schema 1 to schema 2.
|
|
40
|
+
*/
|
|
41
|
+
export async function migrateLmdbSlashingProtectionDatabase(
|
|
42
|
+
dataDirectory: string,
|
|
43
|
+
currentVersion: number,
|
|
44
|
+
latestVersion: number,
|
|
45
|
+
dbMapSizeKb?: number,
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
if (currentVersion !== 1 || latestVersion !== LmdbSlashingProtectionDatabase.SCHEMA_VERSION) {
|
|
48
|
+
throw new Error(`Unsupported LMDB slashing-protection migration ${currentVersion} -> ${latestVersion}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const store = await openStoreAt(dataDirectory, dbMapSizeKb);
|
|
52
|
+
try {
|
|
53
|
+
const duties = store.openMap<string, MigratableStoredDutyRecord>(DUTIES_MAP_NAME);
|
|
54
|
+
const migratedRecords: { key: string; value: StoredDutyRecord }[] = [];
|
|
55
|
+
|
|
56
|
+
for await (const [key, record] of duties.entriesAsync()) {
|
|
57
|
+
if (needsCheckpointNumberMigration(record)) {
|
|
58
|
+
migratedRecords.push({ key, value: { ...record, checkpointNumber: LEGACY_CHECKPOINT_NUMBER } });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (migratedRecords.length > 0) {
|
|
63
|
+
await duties.setMany(migratedRecords);
|
|
64
|
+
}
|
|
65
|
+
} finally {
|
|
66
|
+
await store.close();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function dutyKey(
|
|
71
|
+
rollupAddress: string,
|
|
72
|
+
validatorAddress: string,
|
|
73
|
+
slot: string,
|
|
74
|
+
dutyType: string,
|
|
75
|
+
blockIndexWithinCheckpoint: number,
|
|
76
|
+
): string {
|
|
77
|
+
return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* LMDB-backed implementation of SlashingProtectionDatabase.
|
|
82
|
+
*
|
|
83
|
+
* Provides single-node double-signing protection that survives crashes and restarts.
|
|
84
|
+
* Does not provide cross-node coordination (that requires the PostgreSQL implementation).
|
|
85
|
+
*/
|
|
86
|
+
export class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
87
|
+
public static readonly SCHEMA_VERSION = 2;
|
|
88
|
+
|
|
89
|
+
private readonly duties: AztecAsyncMap<string, StoredDutyRecord>;
|
|
90
|
+
private readonly log: Logger;
|
|
91
|
+
|
|
92
|
+
constructor(
|
|
93
|
+
private readonly store: AztecAsyncKVStore,
|
|
94
|
+
private readonly dateProvider: DateProvider,
|
|
95
|
+
) {
|
|
96
|
+
this.log = createLogger('slashing-protection:lmdb');
|
|
97
|
+
this.duties = store.openMap<string, StoredDutyRecord>(DUTIES_MAP_NAME);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
102
|
+
*
|
|
103
|
+
* LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
|
|
104
|
+
*/
|
|
105
|
+
public async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
|
|
106
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
107
|
+
const key = dutyKey(
|
|
108
|
+
params.rollupAddress.toString(),
|
|
109
|
+
params.validatorAddress.toString(),
|
|
110
|
+
params.slot.toString(),
|
|
111
|
+
params.dutyType,
|
|
112
|
+
blockIndexWithinCheckpoint,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const lockToken = randomBytes(16).toString('hex');
|
|
116
|
+
const now = this.dateProvider.now();
|
|
117
|
+
|
|
118
|
+
const result = await this.store.transactionAsync(async () => {
|
|
119
|
+
const existing = await this.duties.getAsync(key);
|
|
120
|
+
if (existing) {
|
|
121
|
+
return { isNew: false as const, record: { ...existing, lockToken: '' } };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const newRecord: StoredDutyRecord = {
|
|
125
|
+
rollupAddress: params.rollupAddress.toString(),
|
|
126
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
127
|
+
slot: params.slot.toString(),
|
|
128
|
+
blockNumber: params.blockNumber.toString(),
|
|
129
|
+
checkpointNumber: params.checkpointNumber.toString(),
|
|
130
|
+
blockIndexWithinCheckpoint,
|
|
131
|
+
dutyType: params.dutyType,
|
|
132
|
+
status: DutyStatus.SIGNING,
|
|
133
|
+
messageHash: params.messageHash,
|
|
134
|
+
nodeId: params.nodeId,
|
|
135
|
+
lockToken,
|
|
136
|
+
startedAtMs: now,
|
|
137
|
+
};
|
|
138
|
+
await this.duties.set(key, newRecord);
|
|
139
|
+
return { isNew: true as const, record: newRecord };
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
if (result.isNew) {
|
|
143
|
+
this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
|
|
144
|
+
validatorAddress: params.validatorAddress.toString(),
|
|
145
|
+
nodeId: params.nodeId,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { isNew: result.isNew, record: recordFromFields(result.record) };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Update a duty to 'signed' status with the signature.
|
|
154
|
+
* Only succeeds if the lockToken matches.
|
|
155
|
+
*/
|
|
156
|
+
public updateDutySigned(
|
|
157
|
+
rollupAddress: EthAddress,
|
|
158
|
+
validatorAddress: EthAddress,
|
|
159
|
+
slot: SlotNumber,
|
|
160
|
+
dutyType: DutyType,
|
|
161
|
+
signature: string,
|
|
162
|
+
lockToken: string,
|
|
163
|
+
blockIndexWithinCheckpoint: number,
|
|
164
|
+
): Promise<boolean> {
|
|
165
|
+
const key = dutyKey(
|
|
166
|
+
rollupAddress.toString(),
|
|
167
|
+
validatorAddress.toString(),
|
|
168
|
+
slot.toString(),
|
|
169
|
+
dutyType,
|
|
170
|
+
blockIndexWithinCheckpoint,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
return this.store.transactionAsync(async () => {
|
|
174
|
+
const existing = await this.duties.getAsync(key);
|
|
175
|
+
if (!existing) {
|
|
176
|
+
this.log.warn('Failed to update duty to signed: duty not found', {
|
|
177
|
+
rollupAddress: rollupAddress.toString(),
|
|
178
|
+
validatorAddress: validatorAddress.toString(),
|
|
179
|
+
slot: slot.toString(),
|
|
180
|
+
dutyType,
|
|
181
|
+
blockIndexWithinCheckpoint,
|
|
182
|
+
});
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (existing.lockToken !== lockToken) {
|
|
187
|
+
this.log.warn('Failed to update duty to signed: invalid token', {
|
|
188
|
+
rollupAddress: rollupAddress.toString(),
|
|
189
|
+
validatorAddress: validatorAddress.toString(),
|
|
190
|
+
slot: slot.toString(),
|
|
191
|
+
dutyType,
|
|
192
|
+
blockIndexWithinCheckpoint,
|
|
193
|
+
});
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
await this.duties.set(key, {
|
|
198
|
+
...existing,
|
|
199
|
+
status: DutyStatus.SIGNED,
|
|
200
|
+
signature,
|
|
201
|
+
completedAtMs: this.dateProvider.now(),
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return true;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Delete a duty record.
|
|
210
|
+
* Only succeeds if the lockToken matches.
|
|
211
|
+
*/
|
|
212
|
+
public deleteDuty(
|
|
213
|
+
rollupAddress: EthAddress,
|
|
214
|
+
validatorAddress: EthAddress,
|
|
215
|
+
slot: SlotNumber,
|
|
216
|
+
dutyType: DutyType,
|
|
217
|
+
lockToken: string,
|
|
218
|
+
blockIndexWithinCheckpoint: number,
|
|
219
|
+
): Promise<boolean> {
|
|
220
|
+
const key = dutyKey(
|
|
221
|
+
rollupAddress.toString(),
|
|
222
|
+
validatorAddress.toString(),
|
|
223
|
+
slot.toString(),
|
|
224
|
+
dutyType,
|
|
225
|
+
blockIndexWithinCheckpoint,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
return this.store.transactionAsync(async () => {
|
|
229
|
+
const existing = await this.duties.getAsync(key);
|
|
230
|
+
if (!existing || existing.lockToken !== lockToken) {
|
|
231
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
232
|
+
rollupAddress: rollupAddress.toString(),
|
|
233
|
+
validatorAddress: validatorAddress.toString(),
|
|
234
|
+
slot: slot.toString(),
|
|
235
|
+
dutyType,
|
|
236
|
+
blockIndexWithinCheckpoint,
|
|
237
|
+
});
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
await this.duties.delete(key);
|
|
242
|
+
return true;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Cleanup own stuck duties (SIGNING status older than maxAgeMs).
|
|
248
|
+
*/
|
|
249
|
+
public cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
|
|
250
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
251
|
+
|
|
252
|
+
return this.store.transactionAsync(async () => {
|
|
253
|
+
const keysToDelete: string[] = [];
|
|
254
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
255
|
+
if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
|
|
256
|
+
keysToDelete.push(key);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const key of keysToDelete) {
|
|
260
|
+
await this.duties.delete(key);
|
|
261
|
+
}
|
|
262
|
+
return keysToDelete.length;
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Cleanup duties with outdated rollup address.
|
|
268
|
+
*
|
|
269
|
+
* This is always a no-op for the LMDB implementation: the underlying store is created via
|
|
270
|
+
* DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
|
|
271
|
+
* startup whenever the rollup address changes.
|
|
272
|
+
*/
|
|
273
|
+
public cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
|
|
274
|
+
return Promise.resolve(0);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Cleanup old signed duties older than maxAgeMs.
|
|
279
|
+
*/
|
|
280
|
+
public cleanupOldDuties(maxAgeMs: number): Promise<number> {
|
|
281
|
+
const cutoffMs = this.dateProvider.now() - maxAgeMs;
|
|
282
|
+
|
|
283
|
+
return this.store.transactionAsync(async () => {
|
|
284
|
+
const keysToDelete: string[] = [];
|
|
285
|
+
for await (const [key, record] of this.duties.entriesAsync()) {
|
|
286
|
+
if (
|
|
287
|
+
record.status === DutyStatus.SIGNED &&
|
|
288
|
+
record.completedAtMs !== undefined &&
|
|
289
|
+
record.completedAtMs < cutoffMs
|
|
290
|
+
) {
|
|
291
|
+
keysToDelete.push(key);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (const key of keysToDelete) {
|
|
295
|
+
await this.duties.delete(key);
|
|
296
|
+
}
|
|
297
|
+
return keysToDelete.length;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Close the underlying LMDB store.
|
|
303
|
+
*/
|
|
304
|
+
public async close(): Promise<void> {
|
|
305
|
+
await this.store.close();
|
|
306
|
+
this.log.debug('LMDB slashing protection database closed');
|
|
307
|
+
}
|
|
308
|
+
}
|