@aztec/validator-ha-signer 0.0.1-commit.0208eb9
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 +187 -0
- package/dest/config.d.ts +101 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +92 -0
- package/dest/db/index.d.ts +4 -0
- package/dest/db/index.d.ts.map +1 -0
- package/dest/db/index.js +3 -0
- package/dest/db/migrations/1_initial-schema.d.ts +9 -0
- package/dest/db/migrations/1_initial-schema.d.ts.map +1 -0
- package/dest/db/migrations/1_initial-schema.js +20 -0
- package/dest/db/postgres.d.ts +84 -0
- package/dest/db/postgres.d.ts.map +1 -0
- package/dest/db/postgres.js +210 -0
- package/dest/db/schema.d.ts +95 -0
- package/dest/db/schema.d.ts.map +1 -0
- package/dest/db/schema.js +229 -0
- package/dest/db/test_helper.d.ts +10 -0
- package/dest/db/test_helper.d.ts.map +1 -0
- package/dest/db/test_helper.js +14 -0
- package/dest/db/types.d.ts +166 -0
- package/dest/db/types.d.ts.map +1 -0
- package/dest/db/types.js +49 -0
- package/dest/errors.d.ts +34 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +34 -0
- package/dest/factory.d.ts +42 -0
- package/dest/factory.d.ts.map +1 -0
- package/dest/factory.js +70 -0
- package/dest/migrations.d.ts +15 -0
- package/dest/migrations.d.ts.map +1 -0
- package/dest/migrations.js +53 -0
- package/dest/slashing_protection_service.d.ts +84 -0
- package/dest/slashing_protection_service.d.ts.map +1 -0
- package/dest/slashing_protection_service.js +225 -0
- 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 +152 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +21 -0
- package/dest/validator_ha_signer.d.ts +71 -0
- package/dest/validator_ha_signer.d.ts.map +1 -0
- package/dest/validator_ha_signer.js +132 -0
- package/package.json +106 -0
- package/src/config.ts +149 -0
- package/src/db/index.ts +3 -0
- package/src/db/migrations/1_initial-schema.ts +26 -0
- package/src/db/postgres.ts +284 -0
- package/src/db/schema.ts +266 -0
- package/src/db/test_helper.ts +17 -0
- package/src/db/types.ts +206 -0
- package/src/errors.ts +47 -0
- package/src/factory.ts +82 -0
- package/src/migrations.ts +75 -0
- package/src/slashing_protection_service.ts +287 -0
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +226 -0
- package/src/validator_ha_signer.ts +162 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostgreSQL implementation of SlashingProtectionDatabase
|
|
3
|
+
*/ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
5
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
6
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
7
|
+
import { makeBackoff, retry } from '@aztec/foundation/retry';
|
|
8
|
+
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';
|
|
9
|
+
import { getBlockIndexFromDutyIdentifier } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* PostgreSQL implementation of the slashing protection database
|
|
12
|
+
*/ export class PostgresSlashingProtectionDatabase {
|
|
13
|
+
pool;
|
|
14
|
+
log;
|
|
15
|
+
constructor(pool){
|
|
16
|
+
this.pool = pool;
|
|
17
|
+
this.log = createLogger('slashing-protection:postgres');
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Verify that database migrations have been run and schema version matches.
|
|
21
|
+
* Should be called once at startup.
|
|
22
|
+
*
|
|
23
|
+
* @throws Error if migrations haven't been run or schema version is outdated
|
|
24
|
+
*/ async initialize() {
|
|
25
|
+
let dbVersion;
|
|
26
|
+
try {
|
|
27
|
+
const result = await this.pool.query(`SELECT version FROM schema_version ORDER BY version DESC LIMIT 1`);
|
|
28
|
+
if (result.rows.length === 0) {
|
|
29
|
+
throw new Error('No version found');
|
|
30
|
+
}
|
|
31
|
+
dbVersion = result.rows[0].version;
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error('Database schema not initialized. Please run migrations first: aztec migrate-ha-db up --database-url <url>');
|
|
34
|
+
}
|
|
35
|
+
if (dbVersion < SCHEMA_VERSION) {
|
|
36
|
+
throw new Error(`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate-ha-db up --database-url <url>`);
|
|
37
|
+
}
|
|
38
|
+
if (dbVersion > SCHEMA_VERSION) {
|
|
39
|
+
throw new Error(`Database schema version ${dbVersion} is newer than expected (${SCHEMA_VERSION}). Please update your application.`);
|
|
40
|
+
}
|
|
41
|
+
this.log.info('Database schema verified', {
|
|
42
|
+
version: dbVersion
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Atomically try to insert a new duty record, or get the existing one if present.
|
|
47
|
+
*
|
|
48
|
+
* @returns { isNew: true, record } if we successfully inserted and acquired the lock
|
|
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.
|
|
53
|
+
*/ async tryInsertOrGetExisting(params) {
|
|
54
|
+
// create a token for ownership verification
|
|
55
|
+
const lockToken = randomBytes(16).toString('hex');
|
|
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
|
|
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.rollupAddress.toString(),
|
|
67
|
+
params.validatorAddress.toString(),
|
|
68
|
+
params.slot.toString(),
|
|
69
|
+
params.blockNumber.toString(),
|
|
70
|
+
blockIndexWithinCheckpoint,
|
|
71
|
+
params.dutyType,
|
|
72
|
+
params.messageHash,
|
|
73
|
+
params.nodeId,
|
|
74
|
+
lockToken
|
|
75
|
+
]);
|
|
76
|
+
// Throw error if no rows to trigger retry
|
|
77
|
+
if (queryResult.rows.length === 0) {
|
|
78
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows');
|
|
79
|
+
}
|
|
80
|
+
return queryResult;
|
|
81
|
+
}, `INSERT_OR_GET_DUTY for node ${params.nodeId}`, fastBackoff, this.log, true);
|
|
82
|
+
if (result.rows.length === 0) {
|
|
83
|
+
// this should never happen as the retry function should throw if it still fails after retries
|
|
84
|
+
throw new Error('INSERT_OR_GET_DUTY returned no rows after retries');
|
|
85
|
+
}
|
|
86
|
+
if (result.rows.length > 1) {
|
|
87
|
+
// this should never happen if database constraints are correct (PRIMARY KEY should prevent duplicates)
|
|
88
|
+
throw new Error(`INSERT_OR_GET_DUTY returned ${result.rows.length} rows (expected exactly 1).`);
|
|
89
|
+
}
|
|
90
|
+
const row = result.rows[0];
|
|
91
|
+
return {
|
|
92
|
+
isNew: row.is_new,
|
|
93
|
+
record: this.rowToRecord(row)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Update a duty to 'signed' status with the signature.
|
|
98
|
+
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
99
|
+
*
|
|
100
|
+
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
101
|
+
*/ async updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
|
|
102
|
+
const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
|
|
103
|
+
signature,
|
|
104
|
+
rollupAddress.toString(),
|
|
105
|
+
validatorAddress.toString(),
|
|
106
|
+
slot.toString(),
|
|
107
|
+
dutyType,
|
|
108
|
+
blockIndexWithinCheckpoint,
|
|
109
|
+
lockToken
|
|
110
|
+
]);
|
|
111
|
+
if (result.rowCount === 0) {
|
|
112
|
+
this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
|
|
113
|
+
rollupAddress: rollupAddress.toString(),
|
|
114
|
+
validatorAddress: validatorAddress.toString(),
|
|
115
|
+
slot: slot.toString(),
|
|
116
|
+
dutyType,
|
|
117
|
+
blockIndexWithinCheckpoint
|
|
118
|
+
});
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Delete a duty record.
|
|
125
|
+
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
126
|
+
* Used when signing fails to allow another node/attempt to retry.
|
|
127
|
+
*
|
|
128
|
+
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
129
|
+
*/ async deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
|
|
130
|
+
const result = await this.pool.query(DELETE_DUTY, [
|
|
131
|
+
rollupAddress.toString(),
|
|
132
|
+
validatorAddress.toString(),
|
|
133
|
+
slot.toString(),
|
|
134
|
+
dutyType,
|
|
135
|
+
blockIndexWithinCheckpoint,
|
|
136
|
+
lockToken
|
|
137
|
+
]);
|
|
138
|
+
if (result.rowCount === 0) {
|
|
139
|
+
this.log.warn('Failed to delete duty: invalid token or duty not found', {
|
|
140
|
+
rollupAddress: rollupAddress.toString(),
|
|
141
|
+
validatorAddress: validatorAddress.toString(),
|
|
142
|
+
slot: slot.toString(),
|
|
143
|
+
dutyType,
|
|
144
|
+
blockIndexWithinCheckpoint
|
|
145
|
+
});
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Convert a database row to a ValidatorDutyRecord
|
|
152
|
+
*/ rowToRecord(row) {
|
|
153
|
+
return {
|
|
154
|
+
rollupAddress: EthAddress.fromString(row.rollup_address),
|
|
155
|
+
validatorAddress: EthAddress.fromString(row.validator_address),
|
|
156
|
+
slot: SlotNumber.fromString(row.slot),
|
|
157
|
+
blockNumber: BlockNumber.fromString(row.block_number),
|
|
158
|
+
blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
|
|
159
|
+
dutyType: row.duty_type,
|
|
160
|
+
status: row.status,
|
|
161
|
+
messageHash: row.message_hash,
|
|
162
|
+
signature: row.signature ?? undefined,
|
|
163
|
+
nodeId: row.node_id,
|
|
164
|
+
lockToken: row.lock_token,
|
|
165
|
+
startedAt: row.started_at,
|
|
166
|
+
completedAt: row.completed_at ?? undefined,
|
|
167
|
+
errorMessage: row.error_message ?? undefined
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Close the database connection pool
|
|
172
|
+
*/ async close() {
|
|
173
|
+
await this.pool.end();
|
|
174
|
+
this.log.info('Database connection pool closed');
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Cleanup own stuck duties
|
|
178
|
+
* @returns the number of duties cleaned up
|
|
179
|
+
*/ async cleanupOwnStuckDuties(nodeId, maxAgeMs) {
|
|
180
|
+
const cutoff = new Date(Date.now() - maxAgeMs);
|
|
181
|
+
const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [
|
|
182
|
+
nodeId,
|
|
183
|
+
cutoff
|
|
184
|
+
]);
|
|
185
|
+
return result.rowCount ?? 0;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Cleanup duties with outdated rollup address.
|
|
189
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
190
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
191
|
+
* @returns the number of duties cleaned up
|
|
192
|
+
*/ async cleanupOutdatedRollupDuties(currentRollupAddress) {
|
|
193
|
+
const result = await this.pool.query(CLEANUP_OUTDATED_ROLLUP_DUTIES, [
|
|
194
|
+
currentRollupAddress.toString()
|
|
195
|
+
]);
|
|
196
|
+
return result.rowCount ?? 0;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Cleanup old signed duties.
|
|
200
|
+
* Removes only signed duties older than the specified age.
|
|
201
|
+
* Does not remove 'signing' duties as they may be in progress.
|
|
202
|
+
* @returns the number of duties cleaned up
|
|
203
|
+
*/ async cleanupOldDuties(maxAgeMs) {
|
|
204
|
+
const cutoff = new Date(Date.now() - maxAgeMs);
|
|
205
|
+
const result = await this.pool.query(CLEANUP_OLD_DUTIES, [
|
|
206
|
+
cutoff
|
|
207
|
+
]);
|
|
208
|
+
return result.rowCount ?? 0;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL schema for the validator_duties table
|
|
3
|
+
*
|
|
4
|
+
* This table is used for distributed locking and slashing protection across multiple validator nodes.
|
|
5
|
+
* The PRIMARY KEY constraint ensures that only one node can acquire the lock for a given validator,
|
|
6
|
+
* slot, and duty type combination.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Current schema version
|
|
10
|
+
*/
|
|
11
|
+
export declare const SCHEMA_VERSION = 1;
|
|
12
|
+
/**
|
|
13
|
+
* SQL to create the validator_duties table
|
|
14
|
+
*/
|
|
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
|
+
/**
|
|
17
|
+
* SQL to create index on status and started_at for cleanup queries
|
|
18
|
+
*/
|
|
19
|
+
export declare const CREATE_STATUS_INDEX = "\nCREATE INDEX IF NOT EXISTS idx_validator_duties_status\nON validator_duties(status, started_at);\n";
|
|
20
|
+
/**
|
|
21
|
+
* SQL to create index for querying duties by node
|
|
22
|
+
*/
|
|
23
|
+
export declare const CREATE_NODE_INDEX = "\nCREATE INDEX IF NOT EXISTS idx_validator_duties_node\nON validator_duties(node_id, started_at);\n";
|
|
24
|
+
/**
|
|
25
|
+
* SQL to create the schema_version table for tracking migrations
|
|
26
|
+
*/
|
|
27
|
+
export declare const CREATE_SCHEMA_VERSION_TABLE = "\nCREATE TABLE IF NOT EXISTS schema_version (\n version INTEGER PRIMARY KEY,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n);\n";
|
|
28
|
+
/**
|
|
29
|
+
* SQL to initialize schema version
|
|
30
|
+
*/
|
|
31
|
+
export declare const INSERT_SCHEMA_VERSION = "\nINSERT INTO schema_version (version)\nVALUES ($1)\nON CONFLICT (version) DO NOTHING;\n";
|
|
32
|
+
/**
|
|
33
|
+
* Complete schema setup - all statements in order
|
|
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 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
|
+
/**
|
|
37
|
+
* Query to get current schema version
|
|
38
|
+
*/
|
|
39
|
+
export declare const GET_SCHEMA_VERSION = "\nSELECT version FROM schema_version ORDER BY version DESC LIMIT 1;\n";
|
|
40
|
+
/**
|
|
41
|
+
* Atomic insert-or-get query.
|
|
42
|
+
* Tries to insert a new duty record. If a record already exists (conflict),
|
|
43
|
+
* returns the existing record instead.
|
|
44
|
+
*
|
|
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.
|
|
50
|
+
*/
|
|
51
|
+
export declare const INSERT_OR_GET_DUTY = "\nWITH inserted AS (\n INSERT INTO validator_duties (\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n node_id,\n lock_token,\n started_at\n ) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)\n ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING\n RETURNING\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n signature,\n node_id,\n lock_token,\n started_at,\n completed_at,\n error_message,\n TRUE as is_new\n)\nSELECT * FROM inserted\nUNION ALL\nSELECT\n rollup_address,\n validator_address,\n slot,\n block_number,\n block_index_within_checkpoint,\n duty_type,\n status,\n message_hash,\n signature,\n node_id,\n '' as lock_token,\n started_at,\n completed_at,\n error_message,\n FALSE as is_new\nFROM validator_duties\nWHERE rollup_address = $1\n AND validator_address = $2\n AND slot = $3\n AND duty_type = $6\n AND block_index_within_checkpoint = $5\n AND NOT EXISTS (SELECT 1 FROM inserted);\n";
|
|
52
|
+
/**
|
|
53
|
+
* Query to update a duty to 'signed' status
|
|
54
|
+
*/
|
|
55
|
+
export declare const UPDATE_DUTY_SIGNED = "\nUPDATE validator_duties\nSET status = 'signed',\n signature = $1,\n completed_at = CURRENT_TIMESTAMP\nWHERE rollup_address = $2\n AND validator_address = $3\n AND slot = $4\n AND duty_type = $5\n AND block_index_within_checkpoint = $6\n AND status = 'signing'\n AND lock_token = $7;\n";
|
|
56
|
+
/**
|
|
57
|
+
* Query to delete a duty
|
|
58
|
+
* Only deletes if the lockToken matches
|
|
59
|
+
*/
|
|
60
|
+
export declare const DELETE_DUTY = "\nDELETE FROM validator_duties\nWHERE rollup_address = $1\n AND validator_address = $2\n AND slot = $3\n AND duty_type = $4\n AND block_index_within_checkpoint = $5\n AND status = 'signing'\n AND lock_token = $6;\n";
|
|
61
|
+
/**
|
|
62
|
+
* Query to clean up old signed duties (for maintenance)
|
|
63
|
+
* Removes signed duties older than a specified timestamp
|
|
64
|
+
*/
|
|
65
|
+
export declare const CLEANUP_OLD_SIGNED_DUTIES = "\nDELETE FROM validator_duties\nWHERE status = 'signed'\n AND completed_at < $1;\n";
|
|
66
|
+
/**
|
|
67
|
+
* Query to clean up old duties (for maintenance)
|
|
68
|
+
* Removes SIGNED duties older than a specified timestamp
|
|
69
|
+
*/
|
|
70
|
+
export declare const CLEANUP_OLD_DUTIES = "\nDELETE FROM validator_duties\nWHERE status = 'signed'\n AND started_at < $1;\n";
|
|
71
|
+
/**
|
|
72
|
+
* Query to cleanup own stuck duties
|
|
73
|
+
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
74
|
+
*/
|
|
75
|
+
export declare const CLEANUP_OWN_STUCK_DUTIES = "\nDELETE FROM validator_duties\nWHERE node_id = $1\n AND status = 'signing'\n AND started_at < $2;\n";
|
|
76
|
+
/**
|
|
77
|
+
* Query to cleanup duties with outdated rollup address
|
|
78
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
79
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
80
|
+
*/
|
|
81
|
+
export declare const CLEANUP_OUTDATED_ROLLUP_DUTIES = "\nDELETE FROM validator_duties\nWHERE rollup_address != $1;\n";
|
|
82
|
+
/**
|
|
83
|
+
* SQL to drop the validator_duties table
|
|
84
|
+
*/
|
|
85
|
+
export declare const DROP_VALIDATOR_DUTIES_TABLE = "DROP TABLE IF EXISTS validator_duties;";
|
|
86
|
+
/**
|
|
87
|
+
* SQL to drop the schema_version table
|
|
88
|
+
*/
|
|
89
|
+
export declare const DROP_SCHEMA_VERSION_TABLE = "DROP TABLE IF EXISTS schema_version;";
|
|
90
|
+
/**
|
|
91
|
+
* Query to get stuck duties (for monitoring/alerting)
|
|
92
|
+
* Returns duties in 'signing' status that have been stuck for too long
|
|
93
|
+
*/
|
|
94
|
+
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";
|
|
95
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NoZW1hLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGIvc2NoZW1hLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGNBQWMsSUFBSSxDQUFDO0FBRWhDOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDZCQUE2QixzNUJBb0J6QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sbUJBQW1CLHlHQUcvQixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0saUJBQWlCLHdHQUc3QixDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sMkJBQTJCLG1KQUt2QyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0scUJBQXFCLDZGQUlqQyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sWUFBWSxpd0NBS2YsQ0FBQztBQUVYOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGtCQUFrQiwwRUFFOUIsQ0FBQztBQUVGOzs7Ozs7Ozs7O0dBVUc7QUFDSCxlQUFPLE1BQU0sa0JBQWtCLDZ1Q0EwRDlCLENBQUM7QUFFRjs7R0FFRztBQUNILGVBQU8sTUFBTSxrQkFBa0IsK1NBWTlCLENBQUM7QUFFRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sV0FBVyxpT0FTdkIsQ0FBQztBQUVGOzs7R0FHRztBQUNILGVBQU8sTUFBTSx5QkFBeUIsd0ZBSXJDLENBQUM7QUFFRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sa0JBQWtCLHNGQUk5QixDQUFDO0FBRUY7OztHQUdHO0FBQ0gsZUFBTyxNQUFNLHdCQUF3QiwyR0FLcEMsQ0FBQztBQUVGOzs7O0dBSUc7QUFDSCxlQUFPLE1BQU0sOEJBQThCLGtFQUcxQyxDQUFDO0FBRUY7O0dBRUc7QUFDSCxlQUFPLE1BQU0sMkJBQTJCLDJDQUEyQyxDQUFDO0FBRXBGOztHQUVHO0FBQ0gsZUFBTyxNQUFNLHlCQUF5Qix5Q0FBeUMsQ0FBQztBQUVoRjs7O0dBR0c7QUFDSCxlQUFPLE1BQU0sZ0JBQWdCLGtXQWlCNUIsQ0FBQyJ9
|
|
@@ -0,0 +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,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,sFAI9B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,2GAKpC,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"}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL schema for the validator_duties table
|
|
3
|
+
*
|
|
4
|
+
* This table is used for distributed locking and slashing protection across multiple validator nodes.
|
|
5
|
+
* The PRIMARY KEY constraint ensures that only one node can acquire the lock for a given validator,
|
|
6
|
+
* slot, and duty type combination.
|
|
7
|
+
*/ /**
|
|
8
|
+
* Current schema version
|
|
9
|
+
*/ export const SCHEMA_VERSION = 1;
|
|
10
|
+
/**
|
|
11
|
+
* SQL to create the validator_duties table
|
|
12
|
+
*/ export const CREATE_VALIDATOR_DUTIES_TABLE = `
|
|
13
|
+
CREATE TABLE IF NOT EXISTS validator_duties (
|
|
14
|
+
rollup_address VARCHAR(42) NOT NULL,
|
|
15
|
+
validator_address VARCHAR(42) NOT NULL,
|
|
16
|
+
slot BIGINT NOT NULL,
|
|
17
|
+
block_number BIGINT NOT NULL,
|
|
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')),
|
|
21
|
+
message_hash VARCHAR(66) NOT NULL,
|
|
22
|
+
signature VARCHAR(132),
|
|
23
|
+
node_id VARCHAR(255) NOT NULL,
|
|
24
|
+
lock_token VARCHAR(64) NOT NULL,
|
|
25
|
+
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
26
|
+
completed_at TIMESTAMP,
|
|
27
|
+
error_message TEXT,
|
|
28
|
+
|
|
29
|
+
PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
30
|
+
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
31
|
+
);
|
|
32
|
+
`;
|
|
33
|
+
/**
|
|
34
|
+
* SQL to create index on status and started_at for cleanup queries
|
|
35
|
+
*/ export const CREATE_STATUS_INDEX = `
|
|
36
|
+
CREATE INDEX IF NOT EXISTS idx_validator_duties_status
|
|
37
|
+
ON validator_duties(status, started_at);
|
|
38
|
+
`;
|
|
39
|
+
/**
|
|
40
|
+
* SQL to create index for querying duties by node
|
|
41
|
+
*/ export const CREATE_NODE_INDEX = `
|
|
42
|
+
CREATE INDEX IF NOT EXISTS idx_validator_duties_node
|
|
43
|
+
ON validator_duties(node_id, started_at);
|
|
44
|
+
`;
|
|
45
|
+
/**
|
|
46
|
+
* SQL to create the schema_version table for tracking migrations
|
|
47
|
+
*/ export const CREATE_SCHEMA_VERSION_TABLE = `
|
|
48
|
+
CREATE TABLE IF NOT EXISTS schema_version (
|
|
49
|
+
version INTEGER PRIMARY KEY,
|
|
50
|
+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
51
|
+
);
|
|
52
|
+
`;
|
|
53
|
+
/**
|
|
54
|
+
* SQL to initialize schema version
|
|
55
|
+
*/ export const INSERT_SCHEMA_VERSION = `
|
|
56
|
+
INSERT INTO schema_version (version)
|
|
57
|
+
VALUES ($1)
|
|
58
|
+
ON CONFLICT (version) DO NOTHING;
|
|
59
|
+
`;
|
|
60
|
+
/**
|
|
61
|
+
* Complete schema setup - all statements in order
|
|
62
|
+
*/ export const SCHEMA_SETUP = [
|
|
63
|
+
CREATE_SCHEMA_VERSION_TABLE,
|
|
64
|
+
CREATE_VALIDATOR_DUTIES_TABLE,
|
|
65
|
+
CREATE_STATUS_INDEX,
|
|
66
|
+
CREATE_NODE_INDEX
|
|
67
|
+
];
|
|
68
|
+
/**
|
|
69
|
+
* Query to get current schema version
|
|
70
|
+
*/ export const GET_SCHEMA_VERSION = `
|
|
71
|
+
SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
72
|
+
`;
|
|
73
|
+
/**
|
|
74
|
+
* Atomic insert-or-get query.
|
|
75
|
+
* Tries to insert a new duty record. If a record already exists (conflict),
|
|
76
|
+
* returns the existing record instead.
|
|
77
|
+
*
|
|
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.
|
|
83
|
+
*/ export const INSERT_OR_GET_DUTY = `
|
|
84
|
+
WITH inserted AS (
|
|
85
|
+
INSERT INTO validator_duties (
|
|
86
|
+
rollup_address,
|
|
87
|
+
validator_address,
|
|
88
|
+
slot,
|
|
89
|
+
block_number,
|
|
90
|
+
block_index_within_checkpoint,
|
|
91
|
+
duty_type,
|
|
92
|
+
status,
|
|
93
|
+
message_hash,
|
|
94
|
+
node_id,
|
|
95
|
+
lock_token,
|
|
96
|
+
started_at
|
|
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
|
|
99
|
+
RETURNING
|
|
100
|
+
rollup_address,
|
|
101
|
+
validator_address,
|
|
102
|
+
slot,
|
|
103
|
+
block_number,
|
|
104
|
+
block_index_within_checkpoint,
|
|
105
|
+
duty_type,
|
|
106
|
+
status,
|
|
107
|
+
message_hash,
|
|
108
|
+
signature,
|
|
109
|
+
node_id,
|
|
110
|
+
lock_token,
|
|
111
|
+
started_at,
|
|
112
|
+
completed_at,
|
|
113
|
+
error_message,
|
|
114
|
+
TRUE as is_new
|
|
115
|
+
)
|
|
116
|
+
SELECT * FROM inserted
|
|
117
|
+
UNION ALL
|
|
118
|
+
SELECT
|
|
119
|
+
rollup_address,
|
|
120
|
+
validator_address,
|
|
121
|
+
slot,
|
|
122
|
+
block_number,
|
|
123
|
+
block_index_within_checkpoint,
|
|
124
|
+
duty_type,
|
|
125
|
+
status,
|
|
126
|
+
message_hash,
|
|
127
|
+
signature,
|
|
128
|
+
node_id,
|
|
129
|
+
'' as lock_token,
|
|
130
|
+
started_at,
|
|
131
|
+
completed_at,
|
|
132
|
+
error_message,
|
|
133
|
+
FALSE as is_new
|
|
134
|
+
FROM validator_duties
|
|
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
|
|
140
|
+
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
141
|
+
`;
|
|
142
|
+
/**
|
|
143
|
+
* Query to update a duty to 'signed' status
|
|
144
|
+
*/ export const UPDATE_DUTY_SIGNED = `
|
|
145
|
+
UPDATE validator_duties
|
|
146
|
+
SET status = 'signed',
|
|
147
|
+
signature = $1,
|
|
148
|
+
completed_at = CURRENT_TIMESTAMP
|
|
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
|
|
154
|
+
AND status = 'signing'
|
|
155
|
+
AND lock_token = $7;
|
|
156
|
+
`;
|
|
157
|
+
/**
|
|
158
|
+
* Query to delete a duty
|
|
159
|
+
* Only deletes if the lockToken matches
|
|
160
|
+
*/ export const DELETE_DUTY = `
|
|
161
|
+
DELETE FROM validator_duties
|
|
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
|
|
167
|
+
AND status = 'signing'
|
|
168
|
+
AND lock_token = $6;
|
|
169
|
+
`;
|
|
170
|
+
/**
|
|
171
|
+
* Query to clean up old signed duties (for maintenance)
|
|
172
|
+
* Removes signed duties older than a specified timestamp
|
|
173
|
+
*/ export const CLEANUP_OLD_SIGNED_DUTIES = `
|
|
174
|
+
DELETE FROM validator_duties
|
|
175
|
+
WHERE status = 'signed'
|
|
176
|
+
AND completed_at < $1;
|
|
177
|
+
`;
|
|
178
|
+
/**
|
|
179
|
+
* Query to clean up old duties (for maintenance)
|
|
180
|
+
* Removes SIGNED duties older than a specified timestamp
|
|
181
|
+
*/ export const CLEANUP_OLD_DUTIES = `
|
|
182
|
+
DELETE FROM validator_duties
|
|
183
|
+
WHERE status = 'signed'
|
|
184
|
+
AND started_at < $1;
|
|
185
|
+
`;
|
|
186
|
+
/**
|
|
187
|
+
* Query to cleanup own stuck duties
|
|
188
|
+
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
189
|
+
*/ export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
190
|
+
DELETE FROM validator_duties
|
|
191
|
+
WHERE node_id = $1
|
|
192
|
+
AND status = 'signing'
|
|
193
|
+
AND started_at < $2;
|
|
194
|
+
`;
|
|
195
|
+
/**
|
|
196
|
+
* Query to cleanup duties with outdated rollup address
|
|
197
|
+
* Removes all duties where the rollup address doesn't match the current one
|
|
198
|
+
* Used after a rollup upgrade to clean up duties for the old rollup
|
|
199
|
+
*/ export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
|
|
200
|
+
DELETE FROM validator_duties
|
|
201
|
+
WHERE rollup_address != $1;
|
|
202
|
+
`;
|
|
203
|
+
/**
|
|
204
|
+
* SQL to drop the validator_duties table
|
|
205
|
+
*/ export const DROP_VALIDATOR_DUTIES_TABLE = `DROP TABLE IF EXISTS validator_duties;`;
|
|
206
|
+
/**
|
|
207
|
+
* SQL to drop the schema_version table
|
|
208
|
+
*/ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
|
|
209
|
+
/**
|
|
210
|
+
* Query to get stuck duties (for monitoring/alerting)
|
|
211
|
+
* Returns duties in 'signing' status that have been stuck for too long
|
|
212
|
+
*/ export const GET_STUCK_DUTIES = `
|
|
213
|
+
SELECT
|
|
214
|
+
rollup_address,
|
|
215
|
+
validator_address,
|
|
216
|
+
slot,
|
|
217
|
+
block_number,
|
|
218
|
+
block_index_within_checkpoint,
|
|
219
|
+
duty_type,
|
|
220
|
+
status,
|
|
221
|
+
message_hash,
|
|
222
|
+
node_id,
|
|
223
|
+
started_at,
|
|
224
|
+
EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds
|
|
225
|
+
FROM validator_duties
|
|
226
|
+
WHERE status = 'signing'
|
|
227
|
+
AND started_at < $1
|
|
228
|
+
ORDER BY started_at ASC;
|
|
229
|
+
`;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers for database setup
|
|
3
|
+
*/
|
|
4
|
+
import type { PGlite } from '@electric-sql/pglite';
|
|
5
|
+
/**
|
|
6
|
+
* Set up the database schema for testing.
|
|
7
|
+
* This runs the same SQL that migrations would apply.
|
|
8
|
+
*/
|
|
9
|
+
export declare function setupTestSchema(pglite: PGlite): Promise<void>;
|
|
10
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdF9oZWxwZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi90ZXN0X2hlbHBlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUNILE9BQU8sS0FBSyxFQUFFLE1BQU0sRUFBRSxNQUFNLHNCQUFzQixDQUFDO0FBSW5EOzs7R0FHRztBQUNILHdCQUFzQixlQUFlLENBQUMsTUFBTSxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsSUFBSSxDQUFDLENBS25FIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test_helper.d.ts","sourceRoot":"","sources":["../../src/db/test_helper.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAInD;;;GAGG;AACH,wBAAsB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAKnE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test helpers for database setup
|
|
3
|
+
*/ import { INSERT_SCHEMA_VERSION, SCHEMA_SETUP, SCHEMA_VERSION } from './schema.js';
|
|
4
|
+
/**
|
|
5
|
+
* Set up the database schema for testing.
|
|
6
|
+
* This runs the same SQL that migrations would apply.
|
|
7
|
+
*/ export async function setupTestSchema(pglite) {
|
|
8
|
+
for (const statement of SCHEMA_SETUP){
|
|
9
|
+
await pglite.query(statement);
|
|
10
|
+
}
|
|
11
|
+
await pglite.query(INSERT_SCHEMA_VERSION, [
|
|
12
|
+
SCHEMA_VERSION
|
|
13
|
+
]);
|
|
14
|
+
}
|