@aztec/validator-ha-signer 0.0.1-commit.7d4e6cd → 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 +50 -37
- 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 +37 -6
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +86 -28
- package/dest/db/schema.d.ts +21 -10
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +49 -20
- package/dest/db/types.d.ts +109 -33
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +57 -8
- 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 +25 -15
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +55 -15
- package/dest/metrics.d.ts +51 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +103 -0
- 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 +25 -6
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +72 -20
- 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 +40 -16
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -1
- package/dest/validator_ha_signer.d.ts +18 -13
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +45 -36
- package/package.json +15 -10
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +264 -0
- package/src/db/postgres.ts +109 -27
- package/src/db/schema.ts +51 -20
- package/src/db/types.ts +166 -32
- package/src/errors.ts +7 -2
- package/src/factory.ts +67 -15
- package/src/metrics.ts +138 -0
- package/src/migrations.ts +17 -1
- package/src/slashing_protection_service.ts +117 -25
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +65 -16
- package/src/validator_ha_signer.ts +64 -45
- package/dest/config.d.ts +0 -47
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -64
- package/src/config.ts +0 -116
package/dest/db/postgres.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
3
|
*/ import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
4
|
-
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
5
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
6
|
-
import {
|
|
5
|
+
import { makeBackoff, retry } from '@aztec/foundation/retry';
|
|
6
|
+
import { CLEANUP_OLD_DUTIES, CLEANUP_OUTDATED_ROLLUP_DUTIES, CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSION, UPDATE_DUTY_SIGNED } from './schema.js';
|
|
7
|
+
import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
|
|
7
8
|
/**
|
|
8
9
|
* PostgreSQL implementation of the slashing protection database
|
|
9
10
|
*/ export class PostgresSlashingProtectionDatabase {
|
|
@@ -27,10 +28,10 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
27
28
|
}
|
|
28
29
|
dbVersion = result.rows[0].version;
|
|
29
30
|
} catch {
|
|
30
|
-
throw new Error('Database schema not initialized. Please run migrations first: aztec migrate up --database-url <url>');
|
|
31
|
+
throw new Error('Database schema not initialized. Please run migrations first: aztec migrate-ha-db up --database-url <url>');
|
|
31
32
|
}
|
|
32
33
|
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>`);
|
|
34
|
+
throw new Error(`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate-ha-db up --database-url <url>`);
|
|
34
35
|
}
|
|
35
36
|
if (dbVersion > SCHEMA_VERSION) {
|
|
36
37
|
throw new Error(`Database schema version ${dbVersion} is newer than expected (${SCHEMA_VERSION}). Please update your application.`);
|
|
@@ -44,21 +45,45 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
44
45
|
*
|
|
45
46
|
* @returns { isNew: true, record } if we successfully inserted and acquired the lock
|
|
46
47
|
* @returns { isNew: false, record } if a record already exists. lock_token is empty if the record already exists.
|
|
48
|
+
*
|
|
49
|
+
* Retries if no rows are returned, which can happen under high concurrency
|
|
50
|
+
* when another transaction just committed the row but it's not yet visible.
|
|
47
51
|
*/ async tryInsertOrGetExisting(params) {
|
|
48
52
|
// create a token for ownership verification
|
|
49
53
|
const lockToken = randomBytes(16).toString('hex');
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
params.messageHash,
|
|
56
|
-
params.nodeId,
|
|
57
|
-
lockToken
|
|
54
|
+
// Use fast retries with custom backoff: 10ms, 20ms, 30ms (then stop)
|
|
55
|
+
const fastBackoff = makeBackoff([
|
|
56
|
+
0.01,
|
|
57
|
+
0.02,
|
|
58
|
+
0.03
|
|
58
59
|
]);
|
|
60
|
+
// Get the normalized block index using type-safe helper
|
|
61
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
62
|
+
const result = await retry(async ()=>{
|
|
63
|
+
const queryResult = await this.pool.query(INSERT_OR_GET_DUTY, [
|
|
64
|
+
params.rollupAddress.toString(),
|
|
65
|
+
params.validatorAddress.toString(),
|
|
66
|
+
params.slot.toString(),
|
|
67
|
+
params.blockNumber.toString(),
|
|
68
|
+
blockIndexWithinCheckpoint,
|
|
69
|
+
params.dutyType,
|
|
70
|
+
params.messageHash,
|
|
71
|
+
params.nodeId,
|
|
72
|
+
lockToken
|
|
73
|
+
]);
|
|
74
|
+
// Throw error if no rows to trigger retry
|
|
75
|
+
if (queryResult.rows.length === 0) {
|
|
76
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
77
|
+
}
|
|
78
|
+
return queryResult;
|
|
79
|
+
}, `INSERT_OR_GET_DUTY for node ${params.nodeId}`, fastBackoff, this.log, true);
|
|
59
80
|
if (result.rows.length === 0) {
|
|
60
|
-
//
|
|
61
|
-
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
81
|
+
// this should never happen as the retry function should throw if it still fails after retries
|
|
82
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows after retries');
|
|
83
|
+
}
|
|
84
|
+
if (result.rows.length > 1) {
|
|
85
|
+
// this should never happen if database constraints are correct (PRIMARY KEY should prevent duplicates)
|
|
86
|
+
throw new Error(`INSERT_OR_GET_DUTY returned ${result.rows.length} rows (expected exactly 1).`);
|
|
62
87
|
}
|
|
63
88
|
const row = result.rows[0];
|
|
64
89
|
return {
|
|
@@ -71,19 +96,23 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
71
96
|
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
72
97
|
*
|
|
73
98
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
74
|
-
*/ async updateDutySigned(validatorAddress, slot, dutyType, signature, lockToken) {
|
|
99
|
+
*/ async updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
75
100
|
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
76
101
|
signature,
|
|
102
|
+
rollupAddress.toString(),
|
|
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', {
|
|
111
|
+
rollupAddress: rollupAddress.toString(),
|
|
84
112
|
validatorAddress: validatorAddress.toString(),
|
|
85
113
|
slot: slot.toString(),
|
|
86
|
-
dutyType
|
|
114
|
+
dutyType,
|
|
115
|
+
blockIndexWithinCheckpoint
|
|
87
116
|
});
|
|
88
117
|
return false;
|
|
89
118
|
}
|
|
@@ -95,40 +124,48 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
95
124
|
* Used when signing fails to allow another node/attempt to retry.
|
|
96
125
|
*
|
|
97
126
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
98
|
-
*/ async deleteDuty(validatorAddress, slot, dutyType, lockToken) {
|
|
127
|
+
*/ async deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
99
128
|
const result = await this.pool.query(DELETE_DUTY, [
|
|
129
|
+
rollupAddress.toString(),
|
|
100
130
|
validatorAddress.toString(),
|
|
101
131
|
slot.toString(),
|
|
102
132
|
dutyType,
|
|
133
|
+
blockIndexWithinCheckpoint,
|
|
103
134
|
lockToken
|
|
104
135
|
]);
|
|
105
136
|
if (result.rowCount === 0) {
|
|
106
137
|
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
138
|
+
rollupAddress: rollupAddress.toString(),
|
|
107
139
|
validatorAddress: validatorAddress.toString(),
|
|
108
140
|
slot: slot.toString(),
|
|
109
|
-
dutyType
|
|
141
|
+
dutyType,
|
|
142
|
+
blockIndexWithinCheckpoint
|
|
110
143
|
});
|
|
111
144
|
return false;
|
|
112
145
|
}
|
|
113
146
|
return true;
|
|
114
147
|
}
|
|
115
148
|
/**
|
|
116
|
-
* Convert a database row to a ValidatorDutyRecord
|
|
149
|
+
* Convert a database row to a ValidatorDutyRecord.
|
|
150
|
+
* Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
|
|
151
|
+
* then delegates to the shared recordFromFields() converter.
|
|
117
152
|
*/ rowToRecord(row) {
|
|
118
|
-
return {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
153
|
+
return recordFromFields({
|
|
154
|
+
rollupAddress: row.rollup_address,
|
|
155
|
+
validatorAddress: row.validator_address,
|
|
156
|
+
slot: row.slot,
|
|
157
|
+
blockNumber: row.block_number,
|
|
158
|
+
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
122
159
|
dutyType: row.duty_type,
|
|
123
160
|
status: row.status,
|
|
124
161
|
messageHash: row.message_hash,
|
|
125
162
|
signature: row.signature ?? undefined,
|
|
126
163
|
nodeId: row.node_id,
|
|
127
164
|
lockToken: row.lock_token,
|
|
128
|
-
|
|
129
|
-
|
|
165
|
+
startedAtMs: row.started_at.getTime(),
|
|
166
|
+
completedAtMs: row.completed_at?.getTime(),
|
|
130
167
|
errorMessage: row.error_message ?? undefined
|
|
131
|
-
};
|
|
168
|
+
});
|
|
132
169
|
}
|
|
133
170
|
/**
|
|
134
171
|
* Close the database connection pool
|
|
@@ -140,10 +177,31 @@ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSI
|
|
|
140
177
|
* Cleanup own stuck duties
|
|
141
178
|
* @returns the number of duties cleaned up
|
|
142
179
|
*/ async cleanupOwnStuckDuties(nodeId, maxAgeMs) {
|
|
143
|
-
const cutoff = new Date(Date.now() - maxAgeMs);
|
|
144
180
|
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [
|
|
145
181
|
nodeId,
|
|
146
|
-
|
|
182
|
+
maxAgeMs
|
|
183
|
+
]);
|
|
184
|
+
return result.rowCount ?? 0;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Cleanup duties with outdated rollup address.
|
|
188
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
189
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
190
|
+
* @returns the number of duties cleaned up
|
|
191
|
+
*/ async cleanupOutdatedRollupDuties(currentRollupAddress) {
|
|
192
|
+
const result = await this.pool.query(CLEANUP_OUTDATED_ROLLUP_DUTIES, [
|
|
193
|
+
currentRollupAddress.toString()
|
|
194
|
+
]);
|
|
195
|
+
return result.rowCount ?? 0;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Cleanup old signed duties.
|
|
199
|
+
* Removes only signed duties older than the specified age.
|
|
200
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
201
|
+
* @returns the number of duties cleaned up
|
|
202
|
+
*/ async cleanupOldDuties(maxAgeMs) {
|
|
203
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [
|
|
204
|
+
maxAgeMs
|
|
147
205
|
]);
|
|
148
206
|
return result.rowCount ?? 0;
|
|
149
207
|
}
|
package/dest/db/schema.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare const SCHEMA_VERSION = 1;
|
|
|
12
12
|
/**
|
|
13
13
|
* SQL to create the validator_duties table
|
|
14
14
|
*/
|
|
15
|
-
export declare const CREATE_VALIDATOR_DUTIES_TABLE = "\nCREATE TABLE IF NOT EXISTS validator_duties (\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n 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'
|
|
15
|
+
export declare const CREATE_VALIDATOR_DUTIES_TABLE = "\nCREATE TABLE IF NOT EXISTS validator_duties (\n rollup_address VARCHAR(42) NOT NULL,\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,\n duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),\n message_hash VARCHAR(66) NOT NULL,\n signature VARCHAR(132),\n node_id VARCHAR(255) NOT NULL,\n lock_token VARCHAR(64) NOT NULL,\n started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n completed_at TIMESTAMP,\n error_message TEXT,\n\n PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),\n CHECK (completed_at IS NULL OR completed_at >= started_at)\n);\n";
|
|
16
16
|
/**
|
|
17
17
|
* SQL to create index on status and started_at for cleanup queries
|
|
18
18
|
*/
|
|
@@ -32,7 +32,7 @@ export declare const INSERT_SCHEMA_VERSION = "\nINSERT INTO schema_version (vers
|
|
|
32
32
|
/**
|
|
33
33
|
* Complete schema setup - all statements in order
|
|
34
34
|
*/
|
|
35
|
-
export declare const SCHEMA_SETUP: readonly ["\nCREATE TABLE IF NOT EXISTS schema_version (\n version INTEGER PRIMARY KEY,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n);\n", "\nCREATE TABLE IF NOT EXISTS validator_duties (\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n 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'
|
|
35
|
+
export declare const SCHEMA_SETUP: readonly ["\nCREATE TABLE IF NOT EXISTS schema_version (\n version INTEGER PRIMARY KEY,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n);\n", "\nCREATE TABLE IF NOT EXISTS validator_duties (\n rollup_address VARCHAR(42) NOT NULL,\n validator_address VARCHAR(42) NOT NULL,\n slot BIGINT NOT NULL,\n block_number BIGINT NOT NULL,\n block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,\n duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),\n status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),\n message_hash VARCHAR(66) NOT NULL,\n signature VARCHAR(132),\n node_id VARCHAR(255) NOT NULL,\n lock_token VARCHAR(64) NOT NULL,\n started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n completed_at TIMESTAMP,\n error_message TEXT,\n\n PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),\n CHECK (completed_at IS NULL OR completed_at >= started_at)\n);\n", "\nCREATE INDEX IF NOT EXISTS idx_validator_duties_status\nON validator_duties(status, started_at);\n", "\nCREATE INDEX IF NOT EXISTS idx_validator_duties_node\nON validator_duties(node_id, started_at);\n"];
|
|
36
36
|
/**
|
|
37
37
|
* Query to get current schema version
|
|
38
38
|
*/
|
|
@@ -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 rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n lock_token,\n started_at\n ) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)\n ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING\n RETURNING\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n signature,\n node_id,\n lock_token,\n started_at,\n completed_at,\n error_message,\n TRUE as is_new\n)\nSELECT * FROM inserted\nUNION ALL\nSELECT\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n signature,\n node_id,\n '' as lock_token,\n started_at,\n completed_at,\n error_message,\n FALSE as is_new\nFROM validator_duties\nWHERE rollup_address = $1\n AND validator_address = $2\n AND slot = $3\n AND duty_type = $6\n AND block_index_within_checkpoint = $5\n AND NOT EXISTS (SELECT 1 FROM inserted);\n";
|
|
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
|
|
55
|
+
export declare const UPDATE_DUTY_SIGNED = "\nUPDATE validator_duties\nSET status = 'signed',\n signature = $1,\n completed_at = CURRENT_TIMESTAMP\nWHERE rollup_address = $2\n AND validator_address = $3\n AND slot = $4\n AND duty_type = $5\n AND block_index_within_checkpoint = $6\n AND status = 'signing'\n AND lock_token = $7;\n";
|
|
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
|
|
60
|
+
export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE rollup_address = $1\n AND validator_address = $2\n AND slot = $3\n AND duty_type = $4\n AND block_index_within_checkpoint = $5\n AND status = 'signing'\n AND lock_token = $6;\n";
|
|
57
61
|
/**
|
|
58
62
|
* Query to clean up old signed duties (for maintenance)
|
|
59
63
|
* Removes signed duties older than a specified timestamp
|
|
@@ -61,14 +65,21 @@ export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE valida
|
|
|
61
65
|
export declare const CLEANUP_OLD_SIGNED_DUTIES = "\nDELETE FROM validator_duties\nWHERE status = 'signed'\n AND completed_at < $1;\n";
|
|
62
66
|
/**
|
|
63
67
|
* Query to clean up old duties (for maintenance)
|
|
64
|
-
* Removes duties older than a specified
|
|
68
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
65
69
|
*/
|
|
66
|
-
export declare const CLEANUP_OLD_DUTIES = "\nDELETE FROM validator_duties\nWHERE status
|
|
70
|
+
export declare const CLEANUP_OLD_DUTIES = "\nDELETE FROM validator_duties\nWHERE status = 'signed'\n AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;\n";
|
|
67
71
|
/**
|
|
68
72
|
* Query to cleanup own stuck duties
|
|
69
73
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
74
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
75
|
+
*/
|
|
76
|
+
export declare const CLEANUP_OWN_STUCK_DUTIES = "\nDELETE FROM validator_duties\nWHERE node_id = $1\n AND status = 'signing'\n AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;\n";
|
|
77
|
+
/**
|
|
78
|
+
* Query to cleanup duties with outdated rollup address
|
|
79
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
80
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
70
81
|
*/
|
|
71
|
-
export declare const
|
|
82
|
+
export declare const CLEANUP_OUTDATED_ROLLUP_DUTIES = "\nDELETE FROM validator_duties\nWHERE rollup_address != $1;\n";
|
|
72
83
|
/**
|
|
73
84
|
* SQL to drop the validator_duties table
|
|
74
85
|
*/
|
|
@@ -81,5 +92,5 @@ export declare const DROP_SCHEMA_VERSION_TABLE = "DROP TABLE IF EXISTS schema_ve
|
|
|
81
92
|
* Query to get stuck duties (for monitoring/alerting)
|
|
82
93
|
* Returns duties in 'signing' status that have been stuck for too long
|
|
83
94
|
*/
|
|
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,
|
|
95
|
+
export declare const GET_STUCK_DUTIES = "\nSELECT\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n started_at,\n EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds\nFROM validator_duties\nWHERE status = 'signing'\n AND started_at < $1\nORDER BY started_at ASC;\n";
|
|
96
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NoZW1hLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGIvc2NoZW1hLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGNBQWMsSUFBSSxDQUFDO0FBRWhDOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDZCQUE2QixzNUJBb0J6QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sbUJBQW1CLHlHQUcvQixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0saUJBQWlCLHdHQUc3QixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sMkJBQTJCLG1KQUt2QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0scUJBQXFCLDZGQUlqQyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sWUFBWSxpd0NBS2YsQ0FBQztBQUVYOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGtCQUFrQiwwRUFFOUIsQ0FBQztBQUVGOzs7Ozs7Ozs7O0dBVUc7QUFDSCxlQUFPLE1BQU0sa0JBQWtCLDZ1Q0EwRDlCLENBQUM7QUFFRjs7R0FFRztBQUNILGVBQU8sTUFBTSxrQkFBa0IsK1NBWTlCLENBQUM7QUFFRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sV0FBVyxpT0FTdkIsQ0FBQztBQUVGOzs7R0FHRztBQUNILGVBQU8sTUFBTSx5QkFBeUIsd0ZBSXJDLENBQUM7QUFFRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sa0JBQWtCLHlJQUk5QixDQUFDO0FBRUY7Ozs7R0FJRztBQUNILGVBQU8sTUFBTSx3QkFBd0IsOEpBS3BDLENBQUM7QUFFRjs7OztHQUlHO0FBQ0gsZUFBTyxNQUFNLDhCQUE4QixrRUFHMUMsQ0FBQztBQUVGOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDJCQUEyQiwyQ0FBMkMsQ0FBQztBQUVwRjs7R0FFRztBQUNILGVBQU8sTUFBTSx5QkFBeUIseUNBQXlDLENBQUM7QUFFaEY7OztHQUdHO0FBQ0gsZUFBTyxNQUFNLGdCQUFnQixrV0FpQjVCLENBQUMifQ==
|
package/dest/db/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/db/schema.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,IAAI,CAAC;AAEhC;;GAEG;AACH,eAAO,MAAM,6BAA6B,
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/db/schema.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,eAAO,MAAM,cAAc,IAAI,CAAC;AAEhC;;GAEG;AACH,eAAO,MAAM,6BAA6B,s5BAoBzC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,yGAG/B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,wGAG7B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,mJAKvC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,qBAAqB,6FAIjC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,iwCAKf,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,kBAAkB,0EAE9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,6uCA0D9B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,+SAY9B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,iOASvB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,yBAAyB,wFAIrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,yIAI9B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,8JAKpC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,kEAG1C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,2CAA2C,CAAC;AAEpF;;GAEG;AACH,eAAO,MAAM,yBAAyB,yCAAyC,CAAC;AAEhF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,kWAiB5B,CAAC"}
|
package/dest/db/schema.js
CHANGED
|
@@ -11,11 +11,13 @@
|
|
|
11
11
|
* SQL to create the validator_duties table
|
|
12
12
|
*/ export const CREATE_VALIDATOR_DUTIES_TABLE = `
|
|
13
13
|
CREATE TABLE IF NOT EXISTS validator_duties (
|
|
14
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
14
15
|
validator_address VARCHAR(42) NOT NULL,
|
|
15
16
|
slot BIGINT NOT NULL,
|
|
16
17
|
block_number BIGINT NOT NULL,
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
19
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
20
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
|
|
19
21
|
message_hash VARCHAR(66) NOT NULL,
|
|
20
22
|
signature VARCHAR(132),
|
|
21
23
|
node_id VARCHAR(255) NOT NULL,
|
|
@@ -24,7 +26,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
|
|
|
24
26
|
completed_at TIMESTAMP,
|
|
25
27
|
error_message TEXT,
|
|
26
28
|
|
|
27
|
-
PRIMARY KEY (validator_address, slot, duty_type),
|
|
29
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
28
30
|
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
29
31
|
);
|
|
30
32
|
`;
|
|
@@ -74,24 +76,32 @@ SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
|
74
76
|
* returns the existing record instead.
|
|
75
77
|
*
|
|
76
78
|
* Returns the record with an `is_new` flag indicating whether we inserted or got existing.
|
|
79
|
+
*
|
|
80
|
+
* Note: In high concurrency scenarios, if the INSERT conflicts and another transaction
|
|
81
|
+
* just committed the row, there's a small window where the SELECT might not see it yet.
|
|
82
|
+
* The application layer should retry if no rows are returned.
|
|
77
83
|
*/ export const INSERT_OR_GET_DUTY = `
|
|
78
84
|
WITH inserted AS (
|
|
79
85
|
INSERT INTO validator_duties (
|
|
86
|
+
rollup_address,
|
|
80
87
|
validator_address,
|
|
81
88
|
slot,
|
|
82
89
|
block_number,
|
|
90
|
+
block_index_within_checkpoint,
|
|
83
91
|
duty_type,
|
|
84
92
|
status,
|
|
85
93
|
message_hash,
|
|
86
94
|
node_id,
|
|
87
95
|
lock_token,
|
|
88
96
|
started_at
|
|
89
|
-
) VALUES ($1, $2, $3, $4, 'signing', $
|
|
90
|
-
ON CONFLICT (validator_address, slot, duty_type) DO NOTHING
|
|
97
|
+
) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)
|
|
98
|
+
ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
91
99
|
RETURNING
|
|
100
|
+
rollup_address,
|
|
92
101
|
validator_address,
|
|
93
102
|
slot,
|
|
94
103
|
block_number,
|
|
104
|
+
block_index_within_checkpoint,
|
|
95
105
|
duty_type,
|
|
96
106
|
status,
|
|
97
107
|
message_hash,
|
|
@@ -106,9 +116,11 @@ WITH inserted AS (
|
|
|
106
116
|
SELECT * FROM inserted
|
|
107
117
|
UNION ALL
|
|
108
118
|
SELECT
|
|
119
|
+
rollup_address,
|
|
109
120
|
validator_address,
|
|
110
121
|
slot,
|
|
111
122
|
block_number,
|
|
123
|
+
block_index_within_checkpoint,
|
|
112
124
|
duty_type,
|
|
113
125
|
status,
|
|
114
126
|
message_hash,
|
|
@@ -120,9 +132,11 @@ SELECT
|
|
|
120
132
|
error_message,
|
|
121
133
|
FALSE as is_new
|
|
122
134
|
FROM validator_duties
|
|
123
|
-
WHERE
|
|
124
|
-
AND
|
|
125
|
-
AND
|
|
135
|
+
WHERE rollup_address = $1
|
|
136
|
+
AND validator_address = $2
|
|
137
|
+
AND slot = $3
|
|
138
|
+
AND duty_type = $6
|
|
139
|
+
AND block_index_within_checkpoint = $5
|
|
126
140
|
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
127
141
|
`;
|
|
128
142
|
/**
|
|
@@ -132,22 +146,26 @@ UPDATE validator_duties
|
|
|
132
146
|
SET status = 'signed',
|
|
133
147
|
signature = $1,
|
|
134
148
|
completed_at = CURRENT_TIMESTAMP
|
|
135
|
-
WHERE
|
|
136
|
-
AND
|
|
137
|
-
AND
|
|
149
|
+
WHERE rollup_address = $2
|
|
150
|
+
AND validator_address = $3
|
|
151
|
+
AND slot = $4
|
|
152
|
+
AND duty_type = $5
|
|
153
|
+
AND block_index_within_checkpoint = $6
|
|
138
154
|
AND status = 'signing'
|
|
139
|
-
AND lock_token = $
|
|
155
|
+
AND lock_token = $7;
|
|
140
156
|
`;
|
|
141
157
|
/**
|
|
142
158
|
* Query to delete a duty
|
|
143
159
|
* Only deletes if the lockToken matches
|
|
144
160
|
*/ export const DELETE_DUTY = `
|
|
145
161
|
DELETE FROM validator_duties
|
|
146
|
-
WHERE
|
|
147
|
-
AND
|
|
148
|
-
AND
|
|
162
|
+
WHERE rollup_address = $1
|
|
163
|
+
AND validator_address = $2
|
|
164
|
+
AND slot = $3
|
|
165
|
+
AND duty_type = $4
|
|
166
|
+
AND block_index_within_checkpoint = $5
|
|
149
167
|
AND status = 'signing'
|
|
150
|
-
AND lock_token = $
|
|
168
|
+
AND lock_token = $6;
|
|
151
169
|
`;
|
|
152
170
|
/**
|
|
153
171
|
* Query to clean up old signed duties (for maintenance)
|
|
@@ -159,20 +177,29 @@ WHERE status = 'signed'
|
|
|
159
177
|
`;
|
|
160
178
|
/**
|
|
161
179
|
* Query to clean up old duties (for maintenance)
|
|
162
|
-
* Removes duties older than a specified
|
|
180
|
+
* Removes SIGNED duties older than a specified age (in milliseconds)
|
|
163
181
|
*/ export const CLEANUP_OLD_DUTIES = `
|
|
164
182
|
DELETE FROM validator_duties
|
|
165
|
-
WHERE status
|
|
166
|
-
AND started_at < $1;
|
|
183
|
+
WHERE status = 'signed'
|
|
184
|
+
AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
|
|
167
185
|
`;
|
|
168
186
|
/**
|
|
169
187
|
* Query to cleanup own stuck duties
|
|
170
188
|
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
189
|
+
* Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
|
|
171
190
|
*/ export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
172
191
|
DELETE FROM validator_duties
|
|
173
192
|
WHERE node_id = $1
|
|
174
193
|
AND status = 'signing'
|
|
175
|
-
AND started_at < $2;
|
|
194
|
+
AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
|
|
195
|
+
`;
|
|
196
|
+
/**
|
|
197
|
+
* Query to cleanup duties with outdated rollup address
|
|
198
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
199
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
200
|
+
*/ export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
|
|
201
|
+
DELETE FROM validator_duties
|
|
202
|
+
WHERE rollup_address != $1;
|
|
176
203
|
`;
|
|
177
204
|
/**
|
|
178
205
|
* SQL to drop the validator_duties table
|
|
@@ -185,9 +212,11 @@ WHERE node_id = $1
|
|
|
185
212
|
* Returns duties in 'signing' status that have been stuck for too long
|
|
186
213
|
*/ export const GET_STUCK_DUTIES = `
|
|
187
214
|
SELECT
|
|
215
|
+
rollup_address,
|
|
188
216
|
validator_address,
|
|
189
217
|
slot,
|
|
190
218
|
block_number,
|
|
219
|
+
block_index_within_checkpoint,
|
|
191
220
|
duty_type,
|
|
192
221
|
status,
|
|
193
222
|
message_hash,
|