@aztec/validator-ha-signer 0.0.1-commit.96bb3f7 → 0.0.1-commit.e61ad554
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 +42 -37
- package/dest/config.d.ts +49 -17
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +28 -19
- package/dest/db/postgres.d.ts +20 -5
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +50 -19
- package/dest/db/schema.d.ts +11 -7
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +19 -7
- package/dest/db/types.d.ts +75 -23
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +34 -0
- package/dest/errors.d.ts +9 -5
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +7 -4
- package/dest/factory.d.ts +6 -14
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +6 -11
- package/dest/migrations.d.ts +1 -1
- package/dest/migrations.d.ts.map +1 -1
- package/dest/migrations.js +13 -2
- package/dest/slashing_protection_service.d.ts +9 -3
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +21 -9
- package/dest/test/pglite_pool.d.ts +92 -0
- package/dest/test/pglite_pool.d.ts.map +1 -0
- package/dest/test/pglite_pool.js +210 -0
- package/dest/types.d.ts +78 -14
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +21 -1
- package/dest/validator_ha_signer.d.ts +7 -11
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +25 -29
- package/package.json +8 -8
- package/src/config.ts +59 -50
- package/src/db/postgres.ts +68 -19
- package/src/db/schema.ts +19 -7
- package/src/db/types.ts +105 -21
- package/src/errors.ts +7 -2
- package/src/factory.ts +8 -13
- package/src/migrations.ts +17 -1
- package/src/slashing_protection_service.ts +46 -12
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +121 -19
- package/src/validator_ha_signer.ts +32 -39
package/README.md
CHANGED
|
@@ -9,39 +9,21 @@ Distributed locking and slashing protection for Aztec validators running in high
|
|
|
9
9
|
- **Automatic Retry**: Failed signing attempts are cleared, allowing other nodes to retry
|
|
10
10
|
- **PostgreSQL Backend**: Shared database for coordination across nodes
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Integration with Validator Client
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
The HA signer is automatically integrated into the validator client when `VALIDATOR_HA_SIGNING_ENABLED=true` is set. The validator client will:
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
1. Create the HA signer using `createHASigner()` from the factory
|
|
17
|
+
2. Wrap the base keystore with `HAKeyStore` to provide HA-protected signing
|
|
18
|
+
3. Automatically start/stop the signer lifecycle
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
const { signer, db } = await createHASigner({
|
|
21
|
-
databaseUrl: process.env.DATABASE_URL,
|
|
22
|
-
enabled: true,
|
|
23
|
-
nodeId: 'validator-node-1',
|
|
24
|
-
pollingIntervalMs: 100,
|
|
25
|
-
signingTimeoutMs: 3000,
|
|
26
|
-
});
|
|
20
|
+
No manual integration is required when using the validator client.
|
|
27
21
|
|
|
28
|
-
|
|
29
|
-
signer.start();
|
|
22
|
+
## Manual Usage
|
|
30
23
|
|
|
31
|
-
|
|
32
|
-
const signature = await signer.signWithProtection(
|
|
33
|
-
validatorAddress,
|
|
34
|
-
messageHash,
|
|
35
|
-
{ slot: 100n, blockNumber: 50n, dutyType: 'BLOCK_PROPOSAL' },
|
|
36
|
-
async root => localSigner.signMessage(root),
|
|
37
|
-
);
|
|
24
|
+
For advanced use cases or testing, you can use the HA signer directly. **Note**: Database migrations must be run separately before creating the signer (see [Database Migrations](#database-migrations) below).
|
|
38
25
|
|
|
39
|
-
|
|
40
|
-
await signer.stop();
|
|
41
|
-
await db.close();
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Option 2: Manual Migrations (Recommended for Production)
|
|
26
|
+
### Basic Usage
|
|
45
27
|
|
|
46
28
|
```bash
|
|
47
29
|
# 1. Run migrations separately (once per deployment)
|
|
@@ -54,7 +36,7 @@ import { createHASigner } from '@aztec/validator-ha-signer/factory';
|
|
|
54
36
|
|
|
55
37
|
const { signer, db } = await createHASigner({
|
|
56
38
|
databaseUrl: process.env.DATABASE_URL,
|
|
57
|
-
|
|
39
|
+
haSigningEnabled: true,
|
|
58
40
|
nodeId: 'validator-node-1',
|
|
59
41
|
pollingIntervalMs: 100,
|
|
60
42
|
signingTimeoutMs: 3000,
|
|
@@ -63,6 +45,14 @@ const { signer, db } = await createHASigner({
|
|
|
63
45
|
// Start background cleanup tasks
|
|
64
46
|
signer.start();
|
|
65
47
|
|
|
48
|
+
// Sign with protection
|
|
49
|
+
const signature = await signer.signWithProtection(
|
|
50
|
+
validatorAddress,
|
|
51
|
+
messageHash,
|
|
52
|
+
{ slot: 100n, blockNumber: 50n, blockIndexWithinCheckpoint: 0, dutyType: 'BLOCK_PROPOSAL' },
|
|
53
|
+
async root => localSigner.signMessage(root),
|
|
54
|
+
);
|
|
55
|
+
|
|
66
56
|
// On shutdown
|
|
67
57
|
await signer.stop();
|
|
68
58
|
await db.close();
|
|
@@ -73,7 +63,7 @@ await db.close();
|
|
|
73
63
|
If you need custom pool configuration (e.g., max connections, idle timeout) or want to share a connection pool across multiple components:
|
|
74
64
|
|
|
75
65
|
> **Note**: You still need to run migrations separately before using this approach.
|
|
76
|
-
> See [
|
|
66
|
+
> See [Database Migrations](#database-migrations) below.
|
|
77
67
|
|
|
78
68
|
```typescript
|
|
79
69
|
import { PostgresSlashingProtectionDatabase } from '@aztec/validator-ha-signer/db';
|
|
@@ -91,11 +81,11 @@ const db = new PostgresSlashingProtectionDatabase(pool);
|
|
|
91
81
|
await db.initialize();
|
|
92
82
|
|
|
93
83
|
const signer = new ValidatorHASigner(db, {
|
|
94
|
-
|
|
84
|
+
haSigningEnabled: true,
|
|
95
85
|
nodeId: 'validator-node-1',
|
|
96
86
|
pollingIntervalMs: 100,
|
|
97
87
|
signingTimeoutMs: 3000,
|
|
98
|
-
maxStuckDutiesAgeMs:
|
|
88
|
+
maxStuckDutiesAgeMs: 144000,
|
|
99
89
|
});
|
|
100
90
|
|
|
101
91
|
// Start background cleanup tasks
|
|
@@ -111,11 +101,15 @@ await pool.end(); // You manage the pool lifecycle
|
|
|
111
101
|
Set via environment variables or config object:
|
|
112
102
|
|
|
113
103
|
- `VALIDATOR_HA_DATABASE_URL`: PostgreSQL connection string (e.g., `postgresql://user:pass@host:port/db`)
|
|
114
|
-
- `
|
|
115
|
-
- `
|
|
116
|
-
- `
|
|
117
|
-
- `
|
|
118
|
-
- `
|
|
104
|
+
- `VALIDATOR_HA_SIGNING_ENABLED`: Whether HA signing / slashing protection is enabled (default: false)
|
|
105
|
+
- `VALIDATOR_HA_NODE_ID`: Unique identifier for this validator node (required when enabled)
|
|
106
|
+
- `VALIDATOR_HA_POLLING_INTERVAL_MS`: How often to check duty status (default: 100)
|
|
107
|
+
- `VALIDATOR_HA_SIGNING_TIMEOUT_MS`: Max wait for in-progress signing (default: 3000)
|
|
108
|
+
- `VALIDATOR_HA_MAX_STUCK_DUTIES_AGE_MS`: Max age of stuck duties before cleanup (default: 2 \* aztecSlotDuration)
|
|
109
|
+
- `VALIDATOR_HA_POOL_MAX`: Maximum number of connections in the pool (default: 10)
|
|
110
|
+
- `VALIDATOR_HA_POOL_MIN`: Minimum number of connections in the pool (default: 0)
|
|
111
|
+
- `VALIDATOR_HA_POOL_IDLE_TIMEOUT_MS`: Idle timeout for pool connections (default: 10000)
|
|
112
|
+
- `VALIDATOR_HA_POOL_CONNECTION_TIMEOUT_MS`: Connection timeout (default: 0, no timeout)
|
|
119
113
|
|
|
120
114
|
## Database Migrations
|
|
121
115
|
|
|
@@ -170,9 +164,20 @@ When multiple validator nodes attempt to sign:
|
|
|
170
164
|
|
|
171
165
|
1. First node acquires lock and signs
|
|
172
166
|
2. Other nodes receive `DutyAlreadySignedError` (expected)
|
|
173
|
-
3. If different data detected: `SlashingProtectionError` (
|
|
167
|
+
3. If different data detected: `SlashingProtectionError` (prevents slashing)
|
|
174
168
|
4. Failed attempts are auto-cleaned, allowing retry
|
|
175
169
|
|
|
170
|
+
### Signing Context
|
|
171
|
+
|
|
172
|
+
All signing operations require a `SigningContext` that includes:
|
|
173
|
+
|
|
174
|
+
- `slot`: The slot number
|
|
175
|
+
- `blockNumber`: The block number within the checkpoint
|
|
176
|
+
- `blockIndexWithinCheckpoint`: The index of the block within the checkpoint (use `-1` for N/A contexts)
|
|
177
|
+
- `dutyType`: The type of duty (e.g., `BLOCK_PROPOSAL`, `CHECKPOINT_ATTESTATION`, `AUTH_REQUEST`)
|
|
178
|
+
|
|
179
|
+
Note: `AUTH_REQUEST` duties bypass HA protection since signing multiple times is safe for authentication requests.
|
|
180
|
+
|
|
176
181
|
## Development
|
|
177
182
|
|
|
178
183
|
```bash
|
package/dest/config.d.ts
CHANGED
|
@@ -1,30 +1,27 @@
|
|
|
1
1
|
import { type ConfigMappingsType } from '@aztec/foundation/config';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
/**
|
|
3
|
-
* Configuration for the
|
|
4
|
+
* Configuration for the Validator HA Signer
|
|
5
|
+
*
|
|
6
|
+
* This config is used for distributed locking and slashing protection
|
|
7
|
+
* when running multiple validator nodes in a high-availability setup.
|
|
4
8
|
*/
|
|
5
|
-
export interface
|
|
6
|
-
/** Whether slashing protection is enabled */
|
|
7
|
-
|
|
9
|
+
export interface ValidatorHASignerConfig {
|
|
10
|
+
/** Whether HA signing / slashing protection is enabled */
|
|
11
|
+
haSigningEnabled: boolean;
|
|
8
12
|
/** Unique identifier for this node */
|
|
9
13
|
nodeId: string;
|
|
10
14
|
/** How long to wait between polls when a duty is being signed (ms) */
|
|
11
15
|
pollingIntervalMs: number;
|
|
12
16
|
/** Maximum time to wait for a duty being signed to complete (ms) */
|
|
13
17
|
signingTimeoutMs: number;
|
|
14
|
-
/** Maximum age of a stuck duty in ms */
|
|
15
|
-
maxStuckDutiesAgeMs
|
|
16
|
-
}
|
|
17
|
-
export declare const slashingProtectionConfigMappings: ConfigMappingsType<SlashingProtectionConfig>;
|
|
18
|
-
export declare const defaultSlashingProtectionConfig: SlashingProtectionConfig;
|
|
19
|
-
/**
|
|
20
|
-
* Configuration for creating an HA signer with PostgreSQL backend
|
|
21
|
-
*/
|
|
22
|
-
export interface CreateHASignerConfig extends SlashingProtectionConfig {
|
|
18
|
+
/** Maximum age of a stuck duty in ms (defaults to 2x hardcoded Aztec slot duration if not set) */
|
|
19
|
+
maxStuckDutiesAgeMs?: number;
|
|
23
20
|
/**
|
|
24
21
|
* PostgreSQL connection string
|
|
25
22
|
* Format: postgresql://user:password@host:port/database
|
|
26
23
|
*/
|
|
27
|
-
databaseUrl
|
|
24
|
+
databaseUrl?: string;
|
|
28
25
|
/**
|
|
29
26
|
* PostgreSQL connection pool configuration
|
|
30
27
|
*/
|
|
@@ -37,11 +34,46 @@ export interface CreateHASignerConfig extends SlashingProtectionConfig {
|
|
|
37
34
|
/** Connection timeout in milliseconds (default: 0, no timeout) */
|
|
38
35
|
poolConnectionTimeoutMs?: number;
|
|
39
36
|
}
|
|
40
|
-
export declare const
|
|
37
|
+
export declare const validatorHASignerConfigMappings: ConfigMappingsType<ValidatorHASignerConfig>;
|
|
38
|
+
export declare const defaultValidatorHASignerConfig: ValidatorHASignerConfig;
|
|
41
39
|
/**
|
|
42
40
|
* Returns the validator HA signer configuration from environment variables.
|
|
43
41
|
* Note: If an environment variable is not set, the default value is used.
|
|
44
42
|
* @returns The validator HA signer configuration.
|
|
45
43
|
*/
|
|
46
|
-
export declare function getConfigEnvVars():
|
|
47
|
-
|
|
44
|
+
export declare function getConfigEnvVars(): ValidatorHASignerConfig;
|
|
45
|
+
export declare const ValidatorHASignerConfigSchema: z.ZodObject<{
|
|
46
|
+
haSigningEnabled: z.ZodBoolean;
|
|
47
|
+
nodeId: z.ZodString;
|
|
48
|
+
pollingIntervalMs: z.ZodNumber;
|
|
49
|
+
signingTimeoutMs: z.ZodNumber;
|
|
50
|
+
maxStuckDutiesAgeMs: z.ZodOptional<z.ZodNumber>;
|
|
51
|
+
databaseUrl: z.ZodOptional<z.ZodString>;
|
|
52
|
+
poolMaxCount: z.ZodOptional<z.ZodNumber>;
|
|
53
|
+
poolMinCount: z.ZodOptional<z.ZodNumber>;
|
|
54
|
+
poolIdleTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
55
|
+
poolConnectionTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
56
|
+
}, "strip", z.ZodTypeAny, {
|
|
57
|
+
haSigningEnabled: boolean;
|
|
58
|
+
nodeId: string;
|
|
59
|
+
pollingIntervalMs: number;
|
|
60
|
+
signingTimeoutMs: number;
|
|
61
|
+
maxStuckDutiesAgeMs?: number | undefined;
|
|
62
|
+
databaseUrl?: string | undefined;
|
|
63
|
+
poolMaxCount?: number | undefined;
|
|
64
|
+
poolMinCount?: number | undefined;
|
|
65
|
+
poolIdleTimeoutMs?: number | undefined;
|
|
66
|
+
poolConnectionTimeoutMs?: number | undefined;
|
|
67
|
+
}, {
|
|
68
|
+
haSigningEnabled: boolean;
|
|
69
|
+
nodeId: string;
|
|
70
|
+
pollingIntervalMs: number;
|
|
71
|
+
signingTimeoutMs: number;
|
|
72
|
+
maxStuckDutiesAgeMs?: number | undefined;
|
|
73
|
+
databaseUrl?: string | undefined;
|
|
74
|
+
poolMaxCount?: number | undefined;
|
|
75
|
+
poolMinCount?: number | undefined;
|
|
76
|
+
poolIdleTimeoutMs?: number | undefined;
|
|
77
|
+
poolConnectionTimeoutMs?: number | undefined;
|
|
78
|
+
}>;
|
|
79
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQ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0FBQyJ9
|
package/dest/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,EAMxB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,0DAA0D;IAC1D,gBAAgB,EAAE,OAAO,CAAC;IAC1B,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,iBAAiB,EAAE,MAAM,CAAC;IAC1B,oEAAoE;IACpE,gBAAgB,EAAE,MAAM,CAAC;IACzB,kGAAkG;IAClG,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,0DAA0D;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yDAAyD;IACzD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oDAAoD;IACpD,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,kEAAkE;IAClE,uBAAuB,CAAC,EAAE,MAAM,CAAC;CAClC;AAED,eAAO,MAAM,+BAA+B,EAAE,kBAAkB,CAAC,uBAAuB,CAmDvF,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,uBAE5C,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,uBAAuB,CAE1D;AAED,eAAO,MAAM,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAWE,CAAC"}
|
package/dest/config.js
CHANGED
|
@@ -1,35 +1,31 @@
|
|
|
1
|
-
import { booleanConfigHelper, getConfigFromMappings, getDefaultConfig, numberConfigHelper } from '@aztec/foundation/config';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { booleanConfigHelper, getConfigFromMappings, getDefaultConfig, numberConfigHelper, optionalNumberConfigHelper } from '@aztec/foundation/config';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export const validatorHASignerConfigMappings = {
|
|
4
|
+
haSigningEnabled: {
|
|
5
|
+
env: 'VALIDATOR_HA_SIGNING_ENABLED',
|
|
6
|
+
description: 'Whether HA signing / slashing protection is enabled',
|
|
7
|
+
...booleanConfigHelper(false)
|
|
7
8
|
},
|
|
8
9
|
nodeId: {
|
|
9
|
-
env: '
|
|
10
|
+
env: 'VALIDATOR_HA_NODE_ID',
|
|
10
11
|
description: 'The unique identifier for this node',
|
|
11
12
|
defaultValue: ''
|
|
12
13
|
},
|
|
13
14
|
pollingIntervalMs: {
|
|
14
|
-
env: '
|
|
15
|
+
env: 'VALIDATOR_HA_POLLING_INTERVAL_MS',
|
|
15
16
|
description: 'The number of ms to wait between polls when a duty is being signed',
|
|
16
17
|
...numberConfigHelper(100)
|
|
17
18
|
},
|
|
18
19
|
signingTimeoutMs: {
|
|
19
|
-
env: '
|
|
20
|
+
env: 'VALIDATOR_HA_SIGNING_TIMEOUT_MS',
|
|
20
21
|
description: 'The maximum time to wait for a duty being signed to complete',
|
|
21
22
|
...numberConfigHelper(3_000)
|
|
22
23
|
},
|
|
23
24
|
maxStuckDutiesAgeMs: {
|
|
24
|
-
env: '
|
|
25
|
-
description: 'The maximum age of a stuck duty in ms',
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
export const defaultSlashingProtectionConfig = getDefaultConfig(slashingProtectionConfigMappings);
|
|
31
|
-
export const createHASignerConfigMappings = {
|
|
32
|
-
...slashingProtectionConfigMappings,
|
|
25
|
+
env: 'VALIDATOR_HA_MAX_STUCK_DUTIES_AGE_MS',
|
|
26
|
+
description: 'The maximum age of a stuck duty in ms (defaults to 2x Aztec slot duration)',
|
|
27
|
+
...optionalNumberConfigHelper()
|
|
28
|
+
},
|
|
33
29
|
databaseUrl: {
|
|
34
30
|
env: 'VALIDATOR_HA_DATABASE_URL',
|
|
35
31
|
description: 'PostgreSQL connection string for validator HA signer (format: postgresql://user:password@host:port/database)'
|
|
@@ -55,10 +51,23 @@ export const createHASignerConfigMappings = {
|
|
|
55
51
|
...numberConfigHelper(0)
|
|
56
52
|
}
|
|
57
53
|
};
|
|
54
|
+
export const defaultValidatorHASignerConfig = getDefaultConfig(validatorHASignerConfigMappings);
|
|
58
55
|
/**
|
|
59
56
|
* Returns the validator HA signer configuration from environment variables.
|
|
60
57
|
* Note: If an environment variable is not set, the default value is used.
|
|
61
58
|
* @returns The validator HA signer configuration.
|
|
62
59
|
*/ export function getConfigEnvVars() {
|
|
63
|
-
return getConfigFromMappings(
|
|
60
|
+
return getConfigFromMappings(validatorHASignerConfigMappings);
|
|
64
61
|
}
|
|
62
|
+
export const ValidatorHASignerConfigSchema = z.object({
|
|
63
|
+
haSigningEnabled: z.boolean(),
|
|
64
|
+
nodeId: z.string(),
|
|
65
|
+
pollingIntervalMs: z.number().min(0),
|
|
66
|
+
signingTimeoutMs: z.number().min(0),
|
|
67
|
+
maxStuckDutiesAgeMs: z.number().min(0).optional(),
|
|
68
|
+
databaseUrl: z.string().optional(),
|
|
69
|
+
poolMaxCount: z.number().min(0).optional(),
|
|
70
|
+
poolMinCount: z.number().min(0).optional(),
|
|
71
|
+
poolIdleTimeoutMs: z.number().min(0).optional(),
|
|
72
|
+
poolConnectionTimeoutMs: z.number().min(0).optional()
|
|
73
|
+
});
|
package/dest/db/postgres.d.ts
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
|
+
*/
|
|
4
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
1
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
-
import type {
|
|
6
|
+
import type { QueryResult, QueryResultRow } from 'pg';
|
|
3
7
|
import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
|
|
4
8
|
import type { CheckAndRecordParams, DutyType } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Minimal pool interface for database operations.
|
|
11
|
+
* Both pg.Pool and test adapters (e.g., PGlite) satisfy this interface.
|
|
12
|
+
*/
|
|
13
|
+
export interface QueryablePool {
|
|
14
|
+
query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>>;
|
|
15
|
+
end(): Promise<void>;
|
|
16
|
+
}
|
|
5
17
|
/**
|
|
6
18
|
* PostgreSQL implementation of the slashing protection database
|
|
7
19
|
*/
|
|
8
20
|
export declare class PostgresSlashingProtectionDatabase implements SlashingProtectionDatabase {
|
|
9
21
|
private readonly pool;
|
|
10
22
|
private readonly log;
|
|
11
|
-
constructor(pool:
|
|
23
|
+
constructor(pool: QueryablePool);
|
|
12
24
|
/**
|
|
13
25
|
* Verify that database migrations have been run and schema version matches.
|
|
14
26
|
* Should be called once at startup.
|
|
@@ -21,6 +33,9 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
21
33
|
*
|
|
22
34
|
* @returns { isNew: true, record } if we successfully inserted and acquired the lock
|
|
23
35
|
* @returns { isNew: false, record } if a record already exists. lock_token is empty if the record already exists.
|
|
36
|
+
*
|
|
37
|
+
* Retries if no rows are returned, which can happen under high concurrency
|
|
38
|
+
* when another transaction just committed the row but it's not yet visible.
|
|
24
39
|
*/
|
|
25
40
|
tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult>;
|
|
26
41
|
/**
|
|
@@ -29,7 +44,7 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
29
44
|
*
|
|
30
45
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
31
46
|
*/
|
|
32
|
-
updateDutySigned(validatorAddress: EthAddress, slot:
|
|
47
|
+
updateDutySigned(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, signature: string, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
33
48
|
/**
|
|
34
49
|
* Delete a duty record.
|
|
35
50
|
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
@@ -37,7 +52,7 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
37
52
|
*
|
|
38
53
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
39
54
|
*/
|
|
40
|
-
deleteDuty(validatorAddress: EthAddress, slot:
|
|
55
|
+
deleteDuty(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
|
|
41
56
|
/**
|
|
42
57
|
* Convert a database row to a ValidatorDutyRecord
|
|
43
58
|
*/
|
|
@@ -52,4 +67,4 @@ export declare class PostgresSlashingProtectionDatabase implements SlashingProte
|
|
|
52
67
|
*/
|
|
53
68
|
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
54
69
|
}
|
|
55
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
70
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zdGdyZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi9wb3N0Z3Jlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUNILE9BQU8sRUFBZSxVQUFVLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUUxRSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFJM0QsT0FBTyxLQUFLLEVBQUUsV0FBVyxFQUFFLGNBQWMsRUFBRSxNQUFNLElBQUksQ0FBQztBQUV0RCxPQUFPL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0FBQyxPQUFPLENBQUMsQ0FvQmxCO0lBRUQ7Ozs7OztPQU1HO0lBQ0csVUFBVSxDQUNkLGdCQUFnQixFQUFFLFVBQVUsRUFDNUIsSUFBSSxFQUFFLFVBQVUsRUFDaEIsUUFBUSxFQUFFLFFBQVEsRUFDbEIsU0FBUyxFQUFFLE1BQU0sRUFDakIsMEJBQTBCLEVBQUUsTUFBTSxHQUNqQyxPQUFPLENBQUMsT0FBTyxDQUFDLENBbUJsQjtJQUVEOztPQUVHO0lBQ0gsT0FBTyxDQUFDLFdBQVc7SUFrQm5COztPQUVHO0lBQ0csS0FBSyxJQUFJLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FHM0I7SUFFRDs7O09BR0c7SUFDRyxxQkFBcUIsQ0FBQyxNQUFNLEVBQUUsTUFBTSxFQUFFLFFBQVEsRUFBRSxNQUFNLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQUk3RTtDQUNGIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/db/postgres.ts"],"names":[],"mappings":"
|
|
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;AAE1E,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAI3D,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,IAAI,CAAC;AAEtD,OAAO,KAAK,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAQpF,OAAO,KAAK,EAAE,oBAAoB,EAAW,QAAQ,EAAuC,MAAM,YAAY,CAAC;AAG/G;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,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
|
+
*/ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
4
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
5
6
|
import { createLogger } from '@aztec/foundation/log';
|
|
7
|
+
import { makeBackoff, retry } from '@aztec/foundation/retry';
|
|
6
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';
|
|
7
10
|
/**
|
|
8
11
|
* PostgreSQL implementation of the slashing protection database
|
|
9
12
|
*/ export class PostgresSlashingProtectionDatabase {
|
|
@@ -27,10 +30,10 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
27
30
|
}
|
|
28
31
|
dbVersion = result.rows[0].version;
|
|
29
32
|
} catch {
|
|
30
|
-
throw new Error('Database schema not initialized. Please run migrations first: aztec migrate up --database-url <url>');
|
|
33
|
+
throw new Error('Database schema not initialized. Please run migrations first: aztec migrate-ha-db up --database-url <url>');
|
|
31
34
|
}
|
|
32
35
|
if (dbVersion < SCHEMA_VERSION) {
|
|
33
|
-
throw new Error(`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate up --database-url <url>`);
|
|
36
|
+
throw new Error(`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate-ha-db up --database-url <url>`);
|
|
34
37
|
}
|
|
35
38
|
if (dbVersion > SCHEMA_VERSION) {
|
|
36
39
|
throw new Error(`Database schema version ${dbVersion} is newer than expected (${SCHEMA_VERSION}). Please update your application.`);
|
|
@@ -44,21 +47,44 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
44
47
|
*
|
|
45
48
|
* @returns { isNew: true, record } if we successfully inserted and acquired the lock
|
|
46
49
|
* @returns { isNew: false, record } if a record already exists. lock_token is empty if the record already exists.
|
|
50
|
+
*
|
|
51
|
+
* Retries if no rows are returned, which can happen under high concurrency
|
|
52
|
+
* when another transaction just committed the row but it's not yet visible.
|
|
47
53
|
*/ async tryInsertOrGetExisting(params) {
|
|
48
54
|
// create a token for ownership verification
|
|
49
55
|
const lockToken = randomBytes(16).toString('hex');
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
params.messageHash,
|
|
56
|
-
params.nodeId,
|
|
57
|
-
lockToken
|
|
56
|
+
// Use fast retries with custom backoff: 10ms, 20ms, 30ms (then stop)
|
|
57
|
+
const fastBackoff = makeBackoff([
|
|
58
|
+
0.01,
|
|
59
|
+
0.02,
|
|
60
|
+
0.03
|
|
58
61
|
]);
|
|
62
|
+
// Get the normalized block index using type-safe helper
|
|
63
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
64
|
+
const result = await retry(async ()=>{
|
|
65
|
+
const queryResult = await this.pool.query(INSERT_OR_GET_DUTY, [
|
|
66
|
+
params.validatorAddress.toString(),
|
|
67
|
+
params.slot.toString(),
|
|
68
|
+
params.blockNumber.toString(),
|
|
69
|
+
blockIndexWithinCheckpoint,
|
|
70
|
+
params.dutyType,
|
|
71
|
+
params.messageHash,
|
|
72
|
+
params.nodeId,
|
|
73
|
+
lockToken
|
|
74
|
+
]);
|
|
75
|
+
// Throw error if no rows to trigger retry
|
|
76
|
+
if (queryResult.rows.length === 0) {
|
|
77
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
78
|
+
}
|
|
79
|
+
return queryResult;
|
|
80
|
+
}, `INSERT_OR_GET_DUTY for node ${params.nodeId}`, fastBackoff, this.log, true);
|
|
59
81
|
if (result.rows.length === 0) {
|
|
60
|
-
//
|
|
61
|
-
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
82
|
+
// this should never happen as the retry function should throw if it still fails after retries
|
|
83
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows after retries');
|
|
84
|
+
}
|
|
85
|
+
if (result.rows.length > 1) {
|
|
86
|
+
// this should never happen if database constraints are correct (PRIMARY KEY should prevent duplicates)
|
|
87
|
+
throw new Error(`INSERT_OR_GET_DUTY returned ${result.rows.length} rows (expected exactly 1).`);
|
|
62
88
|
}
|
|
63
89
|
const row = result.rows[0];
|
|
64
90
|
return {
|
|
@@ -71,19 +97,21 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
71
97
|
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
72
98
|
*
|
|
73
99
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
74
|
-
*/ async updateDutySigned(validatorAddress, slot, dutyType, signature, lockToken) {
|
|
100
|
+
*/ async updateDutySigned(validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
75
101
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
76
102
|
signature,
|
|
77
103
|
validatorAddress.toString(),
|
|
78
104
|
slot.toString(),
|
|
79
105
|
dutyType,
|
|
106
|
+
blockIndexWithinCheckpoint,
|
|
80
107
|
lockToken
|
|
81
108
|
]);
|
|
82
109
|
if (result.rowCount === 0) {
|
|
83
110
|
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
84
111
|
validatorAddress: validatorAddress.toString(),
|
|
85
112
|
slot: slot.toString(),
|
|
86
|
-
dutyType
|
|
113
|
+
dutyType,
|
|
114
|
+
blockIndexWithinCheckpoint
|
|
87
115
|
});
|
|
88
116
|
return false;
|
|
89
117
|
}
|
|
@@ -95,18 +123,20 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
95
123
|
* Used when signing fails to allow another node/attempt to retry.
|
|
96
124
|
*
|
|
97
125
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
98
|
-
*/ async deleteDuty(validatorAddress, slot, dutyType, lockToken) {
|
|
126
|
+
*/ async deleteDuty(validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
99
127
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
100
128
|
validatorAddress.toString(),
|
|
101
129
|
slot.toString(),
|
|
102
130
|
dutyType,
|
|
131
|
+
blockIndexWithinCheckpoint,
|
|
103
132
|
lockToken
|
|
104
133
|
]);
|
|
105
134
|
if (result.rowCount === 0) {
|
|
106
135
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
107
136
|
validatorAddress: validatorAddress.toString(),
|
|
108
137
|
slot: slot.toString(),
|
|
109
|
-
dutyType
|
|
138
|
+
dutyType,
|
|
139
|
+
blockIndexWithinCheckpoint
|
|
110
140
|
});
|
|
111
141
|
return false;
|
|
112
142
|
}
|
|
@@ -117,8 +147,9 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
117
147
|
*/ rowToRecord(row) {
|
|
118
148
|
return {
|
|
119
149
|
validatorAddress: EthAddress.fromString(row.validator_address),
|
|
120
|
-
slot:
|
|
121
|
-
blockNumber:
|
|
150
|
+
slot: SlotNumber.fromString(row.slot),
|
|
151
|
+
blockNumber: BlockNumber.fromString(row.block_number),
|
|
152
|
+
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
122
153
|
dutyType: row.duty_type,
|
|
123
154
|
status: row.status,
|
|
124
155
|
messageHash: row.message_hash,
|
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 duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed', 'failed')),\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 (validator_address, slot, duty_type),\n CHECK (completed_at IS NULL OR completed_at >= started_at)\n);\n";
|
|
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', 'failed')),\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 (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 duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed', 'failed')),\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 (validator_address, slot, duty_type),\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"];
|
|
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', 'failed')),\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 (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
|
*/
|
|
@@ -43,17 +43,21 @@ export declare const GET_SCHEMA_VERSION = "\nSELECT version FROM schema_version
|
|
|
43
43
|
* returns the existing record instead.
|
|
44
44
|
*
|
|
45
45
|
* Returns the record with an `is_new` flag indicating whether we inserted or got existing.
|
|
46
|
+
*
|
|
47
|
+
* Note: In high concurrency scenarios, if the INSERT conflicts and another transaction
|
|
48
|
+
* just committed the row, there's a small window where the SELECT might not see it yet.
|
|
49
|
+
* The application layer should retry if no rows are returned.
|
|
46
50
|
*/
|
|
47
|
-
export declare const INSERT_OR_GET_DUTY = "\nWITH inserted AS (\n INSERT INTO validator_duties (\n validator_address,\n slot,\n block_number,\n duty_type,\n status,\n message_hash,\n node_id,\n lock_token,\n started_at\n ) VALUES ($1, $2, $3, $4, 'signing', $
|
|
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', $6, $7, $8, CURRENT_TIMESTAMP)\n ON CONFLICT (validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING\n RETURNING\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 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 validator_address = $1\n AND slot = $2\n AND duty_type = $5\n AND block_index_within_checkpoint = $4\n AND NOT EXISTS (SELECT 1 FROM inserted);\n";
|
|
48
52
|
/**
|
|
49
53
|
* Query to update a duty to 'signed' status
|
|
50
54
|
*/
|
|
51
|
-
export declare const UPDATE_DUTY_SIGNED = "\nUPDATE validator_duties\nSET status = 'signed',\n signature = $1,\n completed_at = CURRENT_TIMESTAMP\nWHERE validator_address = $2\n AND slot = $3\n AND duty_type = $4\n AND status = 'signing'\n AND lock_token = $
|
|
55
|
+
export declare const UPDATE_DUTY_SIGNED = "\nUPDATE validator_duties\nSET status = 'signed',\n signature = $1,\n completed_at = CURRENT_TIMESTAMP\nWHERE 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";
|
|
52
56
|
/**
|
|
53
57
|
* Query to delete a duty
|
|
54
58
|
* Only deletes if the lockToken matches
|
|
55
59
|
*/
|
|
56
|
-
export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE validator_address = $1\n AND slot = $2\n AND duty_type = $3\n AND status = 'signing'\n AND lock_token = $
|
|
60
|
+
export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE validator_address = $1\n AND slot = $2\n AND duty_type = $3\n AND block_index_within_checkpoint = $4\n AND status = 'signing'\n AND lock_token = $5;\n";
|
|
57
61
|
/**
|
|
58
62
|
* Query to clean up old signed duties (for maintenance)
|
|
59
63
|
* Removes signed duties older than a specified timestamp
|
|
@@ -81,5 +85,5 @@ export declare const DROP_SCHEMA_VERSION_TABLE = "DROP TABLE IF EXISTS schema_ve
|
|
|
81
85
|
* Query to get stuck duties (for monitoring/alerting)
|
|
82
86
|
* Returns duties in 'signing' status that have been stuck for too long
|
|
83
87
|
*/
|
|
84
|
-
export declare const GET_STUCK_DUTIES = "\nSELECT\n validator_address,\n slot,\n block_number,\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";
|
|
85
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NoZW1hLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGIvc2NoZW1hLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGNBQWMsSUFBSSxDQUFDO0FBRWhDOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDZCQUE2Qix3MkJBbUJ6QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sbUJBQW1CLHlHQUcvQixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU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"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,w2BAmBzC,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,mtCAKf,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,kBAAkB,0EAE9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,ioCAsD9B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,oRAW9B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,sMAQvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,wFAIrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,8GAI9B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,2GAKpC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,2CAA2C,CAAC;AAEpF;;GAEG;AACH,eAAO,MAAM,yBAAyB,yCAAyC,CAAC;AAEhF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,+UAgB5B,CAAC"}
|