@aztec/validator-ha-signer 0.0.1-commit.023c3e5
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 +79 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +73 -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 +70 -0
- package/dest/db/postgres.d.ts.map +1 -0
- package/dest/db/postgres.js +181 -0
- package/dest/db/schema.d.ts +89 -0
- package/dest/db/schema.d.ts.map +1 -0
- package/dest/db/schema.js +213 -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 +161 -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 +80 -0
- package/dest/slashing_protection_service.d.ts.map +1 -0
- package/dest/slashing_protection_service.js +196 -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 +139 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +21 -0
- package/dest/validator_ha_signer.d.ts +70 -0
- package/dest/validator_ha_signer.d.ts.map +1 -0
- package/dest/validator_ha_signer.js +127 -0
- package/package.json +105 -0
- package/src/config.ts +125 -0
- package/src/db/index.ts +3 -0
- package/src/db/migrations/1_initial-schema.ts +26 -0
- package/src/db/postgres.ts +251 -0
- package/src/db/schema.ts +248 -0
- package/src/db/test_helper.ts +17 -0
- package/src/db/types.ts +201 -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 +250 -0
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +207 -0
- package/src/validator_ha_signer.ts +157 -0
|
@@ -0,0 +1,89 @@
|
|
|
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 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
|
+
/**
|
|
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 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
|
+
/**
|
|
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 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";
|
|
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 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";
|
|
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 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";
|
|
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 duties older than a specified timestamp
|
|
69
|
+
*/
|
|
70
|
+
export declare const CLEANUP_OLD_DUTIES = "\nDELETE FROM validator_duties\nWHERE status IN ('signing', 'signed', 'failed')\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
|
+
* SQL to drop the validator_duties table
|
|
78
|
+
*/
|
|
79
|
+
export declare const DROP_VALIDATOR_DUTIES_TABLE = "DROP TABLE IF EXISTS validator_duties;";
|
|
80
|
+
/**
|
|
81
|
+
* SQL to drop the schema_version table
|
|
82
|
+
*/
|
|
83
|
+
export declare const DROP_SCHEMA_VERSION_TABLE = "DROP TABLE IF EXISTS schema_version;";
|
|
84
|
+
/**
|
|
85
|
+
* Query to get stuck duties (for monitoring/alerting)
|
|
86
|
+
* Returns duties in 'signing' status that have been stuck for too long
|
|
87
|
+
*/
|
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2NoZW1hLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGIvc2NoZW1hLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOztHQUVHO0FBQ0gsZUFBTyxNQUFNLGNBQWMsSUFBSSxDQUFDO0FBRWhDOztHQUVHO0FBQ0gsZUFBTyxNQUFNLDZCQUE2Qix3MkJBbUJ6QyxDQ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;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"}
|
|
@@ -0,0 +1,213 @@
|
|
|
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
|
+
validator_address VARCHAR(42) NOT NULL,
|
|
15
|
+
slot BIGINT NOT NULL,
|
|
16
|
+
block_number BIGINT NOT NULL,
|
|
17
|
+
block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
|
|
18
|
+
duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
|
|
19
|
+
status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed', 'failed')),
|
|
20
|
+
message_hash VARCHAR(66) NOT NULL,
|
|
21
|
+
signature VARCHAR(132),
|
|
22
|
+
node_id VARCHAR(255) NOT NULL,
|
|
23
|
+
lock_token VARCHAR(64) NOT NULL,
|
|
24
|
+
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
25
|
+
completed_at TIMESTAMP,
|
|
26
|
+
error_message TEXT,
|
|
27
|
+
|
|
28
|
+
PRIMARY KEY (validator_address, slot, duty_type, block_index_within_checkpoint),
|
|
29
|
+
CHECK (completed_at IS NULL OR completed_at >= started_at)
|
|
30
|
+
);
|
|
31
|
+
`;
|
|
32
|
+
/**
|
|
33
|
+
* SQL to create index on status and started_at for cleanup queries
|
|
34
|
+
*/ export const CREATE_STATUS_INDEX = `
|
|
35
|
+
CREATE INDEX IF NOT EXISTS idx_validator_duties_status
|
|
36
|
+
ON validator_duties(status, started_at);
|
|
37
|
+
`;
|
|
38
|
+
/**
|
|
39
|
+
* SQL to create index for querying duties by node
|
|
40
|
+
*/ export const CREATE_NODE_INDEX = `
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_validator_duties_node
|
|
42
|
+
ON validator_duties(node_id, started_at);
|
|
43
|
+
`;
|
|
44
|
+
/**
|
|
45
|
+
* SQL to create the schema_version table for tracking migrations
|
|
46
|
+
*/ export const CREATE_SCHEMA_VERSION_TABLE = `
|
|
47
|
+
CREATE TABLE IF NOT EXISTS schema_version (
|
|
48
|
+
version INTEGER PRIMARY KEY,
|
|
49
|
+
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
50
|
+
);
|
|
51
|
+
`;
|
|
52
|
+
/**
|
|
53
|
+
* SQL to initialize schema version
|
|
54
|
+
*/ export const INSERT_SCHEMA_VERSION = `
|
|
55
|
+
INSERT INTO schema_version (version)
|
|
56
|
+
VALUES ($1)
|
|
57
|
+
ON CONFLICT (version) DO NOTHING;
|
|
58
|
+
`;
|
|
59
|
+
/**
|
|
60
|
+
* Complete schema setup - all statements in order
|
|
61
|
+
*/ export const SCHEMA_SETUP = [
|
|
62
|
+
CREATE_SCHEMA_VERSION_TABLE,
|
|
63
|
+
CREATE_VALIDATOR_DUTIES_TABLE,
|
|
64
|
+
CREATE_STATUS_INDEX,
|
|
65
|
+
CREATE_NODE_INDEX
|
|
66
|
+
];
|
|
67
|
+
/**
|
|
68
|
+
* Query to get current schema version
|
|
69
|
+
*/ export const GET_SCHEMA_VERSION = `
|
|
70
|
+
SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
|
|
71
|
+
`;
|
|
72
|
+
/**
|
|
73
|
+
* Atomic insert-or-get query.
|
|
74
|
+
* Tries to insert a new duty record. If a record already exists (conflict),
|
|
75
|
+
* returns the existing record instead.
|
|
76
|
+
*
|
|
77
|
+
* Returns the record with an `is_new` flag indicating whether we inserted or got existing.
|
|
78
|
+
*
|
|
79
|
+
* Note: In high concurrency scenarios, if the INSERT conflicts and another transaction
|
|
80
|
+
* just committed the row, there's a small window where the SELECT might not see it yet.
|
|
81
|
+
* The application layer should retry if no rows are returned.
|
|
82
|
+
*/ export const INSERT_OR_GET_DUTY = `
|
|
83
|
+
WITH inserted AS (
|
|
84
|
+
INSERT INTO validator_duties (
|
|
85
|
+
validator_address,
|
|
86
|
+
slot,
|
|
87
|
+
block_number,
|
|
88
|
+
block_index_within_checkpoint,
|
|
89
|
+
duty_type,
|
|
90
|
+
status,
|
|
91
|
+
message_hash,
|
|
92
|
+
node_id,
|
|
93
|
+
lock_token,
|
|
94
|
+
started_at
|
|
95
|
+
) VALUES ($1, $2, $3, $4, $5, 'signing', $6, $7, $8, CURRENT_TIMESTAMP)
|
|
96
|
+
ON CONFLICT (validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
|
|
97
|
+
RETURNING
|
|
98
|
+
validator_address,
|
|
99
|
+
slot,
|
|
100
|
+
block_number,
|
|
101
|
+
block_index_within_checkpoint,
|
|
102
|
+
duty_type,
|
|
103
|
+
status,
|
|
104
|
+
message_hash,
|
|
105
|
+
signature,
|
|
106
|
+
node_id,
|
|
107
|
+
lock_token,
|
|
108
|
+
started_at,
|
|
109
|
+
completed_at,
|
|
110
|
+
error_message,
|
|
111
|
+
TRUE as is_new
|
|
112
|
+
)
|
|
113
|
+
SELECT * FROM inserted
|
|
114
|
+
UNION ALL
|
|
115
|
+
SELECT
|
|
116
|
+
validator_address,
|
|
117
|
+
slot,
|
|
118
|
+
block_number,
|
|
119
|
+
block_index_within_checkpoint,
|
|
120
|
+
duty_type,
|
|
121
|
+
status,
|
|
122
|
+
message_hash,
|
|
123
|
+
signature,
|
|
124
|
+
node_id,
|
|
125
|
+
'' as lock_token,
|
|
126
|
+
started_at,
|
|
127
|
+
completed_at,
|
|
128
|
+
error_message,
|
|
129
|
+
FALSE as is_new
|
|
130
|
+
FROM validator_duties
|
|
131
|
+
WHERE validator_address = $1
|
|
132
|
+
AND slot = $2
|
|
133
|
+
AND duty_type = $5
|
|
134
|
+
AND block_index_within_checkpoint = $4
|
|
135
|
+
AND NOT EXISTS (SELECT 1 FROM inserted);
|
|
136
|
+
`;
|
|
137
|
+
/**
|
|
138
|
+
* Query to update a duty to 'signed' status
|
|
139
|
+
*/ export const UPDATE_DUTY_SIGNED = `
|
|
140
|
+
UPDATE validator_duties
|
|
141
|
+
SET status = 'signed',
|
|
142
|
+
signature = $1,
|
|
143
|
+
completed_at = CURRENT_TIMESTAMP
|
|
144
|
+
WHERE validator_address = $2
|
|
145
|
+
AND slot = $3
|
|
146
|
+
AND duty_type = $4
|
|
147
|
+
AND block_index_within_checkpoint = $5
|
|
148
|
+
AND status = 'signing'
|
|
149
|
+
AND lock_token = $6;
|
|
150
|
+
`;
|
|
151
|
+
/**
|
|
152
|
+
* Query to delete a duty
|
|
153
|
+
* Only deletes if the lockToken matches
|
|
154
|
+
*/ export const DELETE_DUTY = `
|
|
155
|
+
DELETE FROM validator_duties
|
|
156
|
+
WHERE validator_address = $1
|
|
157
|
+
AND slot = $2
|
|
158
|
+
AND duty_type = $3
|
|
159
|
+
AND block_index_within_checkpoint = $4
|
|
160
|
+
AND status = 'signing'
|
|
161
|
+
AND lock_token = $5;
|
|
162
|
+
`;
|
|
163
|
+
/**
|
|
164
|
+
* Query to clean up old signed duties (for maintenance)
|
|
165
|
+
* Removes signed duties older than a specified timestamp
|
|
166
|
+
*/ export const CLEANUP_OLD_SIGNED_DUTIES = `
|
|
167
|
+
DELETE FROM validator_duties
|
|
168
|
+
WHERE status = 'signed'
|
|
169
|
+
AND completed_at < $1;
|
|
170
|
+
`;
|
|
171
|
+
/**
|
|
172
|
+
* Query to clean up old duties (for maintenance)
|
|
173
|
+
* Removes duties older than a specified timestamp
|
|
174
|
+
*/ export const CLEANUP_OLD_DUTIES = `
|
|
175
|
+
DELETE FROM validator_duties
|
|
176
|
+
WHERE status IN ('signing', 'signed', 'failed')
|
|
177
|
+
AND started_at < $1;
|
|
178
|
+
`;
|
|
179
|
+
/**
|
|
180
|
+
* Query to cleanup own stuck duties
|
|
181
|
+
* Removes duties in 'signing' status for a specific node that are older than maxAgeMs
|
|
182
|
+
*/ export const CLEANUP_OWN_STUCK_DUTIES = `
|
|
183
|
+
DELETE FROM validator_duties
|
|
184
|
+
WHERE node_id = $1
|
|
185
|
+
AND status = 'signing'
|
|
186
|
+
AND started_at < $2;
|
|
187
|
+
`;
|
|
188
|
+
/**
|
|
189
|
+
* SQL to drop the validator_duties table
|
|
190
|
+
*/ export const DROP_VALIDATOR_DUTIES_TABLE = `DROP TABLE IF EXISTS validator_duties;`;
|
|
191
|
+
/**
|
|
192
|
+
* SQL to drop the schema_version table
|
|
193
|
+
*/ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
|
|
194
|
+
/**
|
|
195
|
+
* Query to get stuck duties (for monitoring/alerting)
|
|
196
|
+
* Returns duties in 'signing' status that have been stuck for too long
|
|
197
|
+
*/ export const GET_STUCK_DUTIES = `
|
|
198
|
+
SELECT
|
|
199
|
+
validator_address,
|
|
200
|
+
slot,
|
|
201
|
+
block_number,
|
|
202
|
+
block_index_within_checkpoint,
|
|
203
|
+
duty_type,
|
|
204
|
+
status,
|
|
205
|
+
message_hash,
|
|
206
|
+
node_id,
|
|
207
|
+
started_at,
|
|
208
|
+
EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds
|
|
209
|
+
FROM validator_duties
|
|
210
|
+
WHERE status = 'signing'
|
|
211
|
+
AND started_at < $1
|
|
212
|
+
ORDER BY started_at ASC;
|
|
213
|
+
`;
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
|
|
2
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
|
+
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
4
|
+
/**
|
|
5
|
+
* Row type from PostgreSQL query
|
|
6
|
+
*/
|
|
7
|
+
export interface DutyRow {
|
|
8
|
+
validator_address: string;
|
|
9
|
+
slot: string;
|
|
10
|
+
block_number: string;
|
|
11
|
+
block_index_within_checkpoint: number;
|
|
12
|
+
duty_type: DutyType;
|
|
13
|
+
status: DutyStatus;
|
|
14
|
+
message_hash: string;
|
|
15
|
+
signature: string | null;
|
|
16
|
+
node_id: string;
|
|
17
|
+
lock_token: string;
|
|
18
|
+
started_at: Date;
|
|
19
|
+
completed_at: Date | null;
|
|
20
|
+
error_message: string | null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
|
|
24
|
+
*/
|
|
25
|
+
export interface InsertOrGetRow extends DutyRow {
|
|
26
|
+
is_new: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Type of validator duty being performed
|
|
30
|
+
*/
|
|
31
|
+
export declare enum DutyType {
|
|
32
|
+
BLOCK_PROPOSAL = "BLOCK_PROPOSAL",
|
|
33
|
+
CHECKPOINT_PROPOSAL = "CHECKPOINT_PROPOSAL",
|
|
34
|
+
ATTESTATION = "ATTESTATION",
|
|
35
|
+
ATTESTATIONS_AND_SIGNERS = "ATTESTATIONS_AND_SIGNERS",
|
|
36
|
+
GOVERNANCE_VOTE = "GOVERNANCE_VOTE",
|
|
37
|
+
SLASHING_VOTE = "SLASHING_VOTE",
|
|
38
|
+
AUTH_REQUEST = "AUTH_REQUEST",
|
|
39
|
+
TXS = "TXS"
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Status of a duty in the database
|
|
43
|
+
*/
|
|
44
|
+
export declare enum DutyStatus {
|
|
45
|
+
SIGNING = "signing",
|
|
46
|
+
SIGNED = "signed"
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Record of a validator duty in the database
|
|
50
|
+
*/
|
|
51
|
+
export interface ValidatorDutyRecord {
|
|
52
|
+
/** Ethereum address of the validator */
|
|
53
|
+
validatorAddress: EthAddress;
|
|
54
|
+
/** Slot number for this duty */
|
|
55
|
+
slot: SlotNumber;
|
|
56
|
+
/** Block number for this duty */
|
|
57
|
+
blockNumber: BlockNumber;
|
|
58
|
+
/** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
|
|
59
|
+
blockIndexWithinCheckpoint: number;
|
|
60
|
+
/** Type of duty being performed */
|
|
61
|
+
dutyType: DutyType;
|
|
62
|
+
/** Current status of the duty */
|
|
63
|
+
status: DutyStatus;
|
|
64
|
+
/** The signing root (hash) for this duty */
|
|
65
|
+
messageHash: string;
|
|
66
|
+
/** The signature (populated after successful signing) */
|
|
67
|
+
signature?: string;
|
|
68
|
+
/** Unique identifier for the node that acquired the lock */
|
|
69
|
+
nodeId: string;
|
|
70
|
+
/** Secret token for verifying ownership of the duty lock */
|
|
71
|
+
lockToken: string;
|
|
72
|
+
/** When the duty signing was started */
|
|
73
|
+
startedAt: Date;
|
|
74
|
+
/** When the duty signing was completed (success or failure) */
|
|
75
|
+
completedAt?: Date;
|
|
76
|
+
/** Error message if status is 'failed' */
|
|
77
|
+
errorMessage?: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Duty identifier for block proposals.
|
|
81
|
+
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
82
|
+
*/
|
|
83
|
+
export interface BlockProposalDutyIdentifier {
|
|
84
|
+
validatorAddress: EthAddress;
|
|
85
|
+
slot: SlotNumber;
|
|
86
|
+
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
87
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint;
|
|
88
|
+
dutyType: DutyType.BLOCK_PROPOSAL;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Duty identifier for non-block-proposal duties.
|
|
92
|
+
* blockIndexWithinCheckpoint is not applicable (internally stored as -1).
|
|
93
|
+
*/
|
|
94
|
+
export interface OtherDutyIdentifier {
|
|
95
|
+
validatorAddress: EthAddress;
|
|
96
|
+
slot: SlotNumber;
|
|
97
|
+
dutyType: DutyType.CHECKPOINT_PROPOSAL | DutyType.ATTESTATION | DutyType.ATTESTATIONS_AND_SIGNERS | DutyType.GOVERNANCE_VOTE | DutyType.SLASHING_VOTE | DutyType.AUTH_REQUEST | DutyType.TXS;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Minimal info needed to identify a unique duty.
|
|
101
|
+
* Uses discriminated union to enforce type safety:
|
|
102
|
+
* - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
|
|
103
|
+
* - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
|
|
104
|
+
*/
|
|
105
|
+
export type DutyIdentifier = BlockProposalDutyIdentifier | OtherDutyIdentifier;
|
|
106
|
+
/**
|
|
107
|
+
* Validates and normalizes the block index for a duty.
|
|
108
|
+
* - BLOCK_PROPOSAL: validates blockIndexWithinCheckpoint is provided and >= 0
|
|
109
|
+
* - Other duty types: always returns -1
|
|
110
|
+
*
|
|
111
|
+
* @throws Error if BLOCK_PROPOSAL is missing blockIndexWithinCheckpoint or has invalid value
|
|
112
|
+
*/
|
|
113
|
+
export declare function normalizeBlockIndex(dutyType: DutyType, blockIndexWithinCheckpoint: number | undefined): number;
|
|
114
|
+
/**
|
|
115
|
+
* Gets the block index from a DutyIdentifier.
|
|
116
|
+
* - BLOCK_PROPOSAL: returns the blockIndexWithinCheckpoint
|
|
117
|
+
* - Other duty types: returns -1
|
|
118
|
+
*/
|
|
119
|
+
export declare function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number;
|
|
120
|
+
/**
|
|
121
|
+
* Additional parameters for checking and recording a new duty
|
|
122
|
+
*/
|
|
123
|
+
interface CheckAndRecordExtra {
|
|
124
|
+
/** Block number for this duty */
|
|
125
|
+
blockNumber: BlockNumber | CheckpointNumber;
|
|
126
|
+
/** The signing root (hash) for this duty */
|
|
127
|
+
messageHash: string;
|
|
128
|
+
/** Identifier for the node that acquired the lock */
|
|
129
|
+
nodeId: string;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Parameters for checking and recording a new duty.
|
|
133
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
134
|
+
*/
|
|
135
|
+
export type CheckAndRecordParams = DutyIdentifier & CheckAndRecordExtra;
|
|
136
|
+
/**
|
|
137
|
+
* Additional parameters for recording a successful signing
|
|
138
|
+
*/
|
|
139
|
+
interface RecordSuccessExtra {
|
|
140
|
+
signature: Signature;
|
|
141
|
+
nodeId: string;
|
|
142
|
+
lockToken: string;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Parameters for recording a successful signing.
|
|
146
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
147
|
+
*/
|
|
148
|
+
export type RecordSuccessParams = DutyIdentifier & RecordSuccessExtra;
|
|
149
|
+
/**
|
|
150
|
+
* Additional parameters for deleting a duty
|
|
151
|
+
*/
|
|
152
|
+
interface DeleteDutyExtra {
|
|
153
|
+
lockToken: string;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Parameters for deleting a duty.
|
|
157
|
+
* Uses intersection with DutyIdentifier to preserve the discriminated union.
|
|
158
|
+
*/
|
|
159
|
+
export type DeleteDutyParams = DutyIdentifier & DeleteDutyExtra;
|
|
160
|
+
export {};
|
|
161
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi90eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsZ0JBQWdCLEVBQUUscUJBQXFCLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFDeEgsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDaEUsT0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5B,QAAQ,CAAC,WAAW,GACpB,QAAQ,CAAC,wBAAwB,GACjC,QAAQ,CAAC,eAAe,GACxB,QAAQ,CAAC,aAAa,GACtB,QAAQ,CAAC,YAAY,GACrB,QAAQ,CAAC,GAAG,CAAC;CAClB;AAED;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG,2BAA2B,GAAG,mBAAmB,CAAC;AAE/E;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,0BAA0B,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAc9G;AAED;;;;GAIG;AACH,wBAAgB,+BAA+B,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,CAK5E;AAED;;GAEG;AACH,UAAU,mBAAmB;IAC3B,iCAAiC;IACjC,WAAW,EAAE,WAAW,GAAG,gBAAgB,CAAC;IAC5C,4CAA4C;IAC5C,WAAW,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,cAAc,GAAG,mBAAmB,CAAC;AAExE;;GAEG;AACH,UAAU,kBAAkB;IAC1B,SAAS,EAAE,SAAS,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,kBAAkB,CAAC;AAEtE;;GAEG;AACH,UAAU,eAAe;IACvB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,cAAc,GAAG,eAAe,CAAC"}
|
package/dest/db/types.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type of validator duty being performed
|
|
3
|
+
*/ export var DutyType = /*#__PURE__*/ function(DutyType) {
|
|
4
|
+
DutyType["BLOCK_PROPOSAL"] = "BLOCK_PROPOSAL";
|
|
5
|
+
DutyType["CHECKPOINT_PROPOSAL"] = "CHECKPOINT_PROPOSAL";
|
|
6
|
+
DutyType["ATTESTATION"] = "ATTESTATION";
|
|
7
|
+
DutyType["ATTESTATIONS_AND_SIGNERS"] = "ATTESTATIONS_AND_SIGNERS";
|
|
8
|
+
DutyType["GOVERNANCE_VOTE"] = "GOVERNANCE_VOTE";
|
|
9
|
+
DutyType["SLASHING_VOTE"] = "SLASHING_VOTE";
|
|
10
|
+
DutyType["AUTH_REQUEST"] = "AUTH_REQUEST";
|
|
11
|
+
DutyType["TXS"] = "TXS";
|
|
12
|
+
return DutyType;
|
|
13
|
+
}({});
|
|
14
|
+
/**
|
|
15
|
+
* Status of a duty in the database
|
|
16
|
+
*/ export var DutyStatus = /*#__PURE__*/ function(DutyStatus) {
|
|
17
|
+
DutyStatus["SIGNING"] = "signing";
|
|
18
|
+
DutyStatus["SIGNED"] = "signed";
|
|
19
|
+
return DutyStatus;
|
|
20
|
+
}({});
|
|
21
|
+
/**
|
|
22
|
+
* Validates and normalizes the block index for a duty.
|
|
23
|
+
* - BLOCK_PROPOSAL: validates blockIndexWithinCheckpoint is provided and >= 0
|
|
24
|
+
* - Other duty types: always returns -1
|
|
25
|
+
*
|
|
26
|
+
* @throws Error if BLOCK_PROPOSAL is missing blockIndexWithinCheckpoint or has invalid value
|
|
27
|
+
*/ export function normalizeBlockIndex(dutyType, blockIndexWithinCheckpoint) {
|
|
28
|
+
if (dutyType === "BLOCK_PROPOSAL") {
|
|
29
|
+
if (blockIndexWithinCheckpoint === undefined) {
|
|
30
|
+
throw new Error('BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint to be specified');
|
|
31
|
+
}
|
|
32
|
+
if (blockIndexWithinCheckpoint < 0) {
|
|
33
|
+
throw new Error(`BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint >= 0, got ${blockIndexWithinCheckpoint}`);
|
|
34
|
+
}
|
|
35
|
+
return blockIndexWithinCheckpoint;
|
|
36
|
+
}
|
|
37
|
+
// For all other duty types, always use -1
|
|
38
|
+
return -1;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Gets the block index from a DutyIdentifier.
|
|
42
|
+
* - BLOCK_PROPOSAL: returns the blockIndexWithinCheckpoint
|
|
43
|
+
* - Other duty types: returns -1
|
|
44
|
+
*/ export function getBlockIndexFromDutyIdentifier(duty) {
|
|
45
|
+
if (duty.dutyType === "BLOCK_PROPOSAL") {
|
|
46
|
+
return duty.blockIndexWithinCheckpoint;
|
|
47
|
+
}
|
|
48
|
+
return -1;
|
|
49
|
+
}
|
package/dest/errors.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom errors for the validator HA signer
|
|
3
|
+
*/
|
|
4
|
+
import type { SlotNumber } from '@aztec/foundation/branded-types';
|
|
5
|
+
import type { DutyType } from './db/types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Thrown when a duty has already been signed (by any node).
|
|
8
|
+
* This is expected behavior in an HA setup - all nodes try to sign,
|
|
9
|
+
* the first one wins, and subsequent attempts get this error.
|
|
10
|
+
*/
|
|
11
|
+
export declare class DutyAlreadySignedError extends Error {
|
|
12
|
+
readonly slot: SlotNumber;
|
|
13
|
+
readonly dutyType: DutyType;
|
|
14
|
+
readonly blockIndexWithinCheckpoint: number;
|
|
15
|
+
readonly signedByNode: string;
|
|
16
|
+
constructor(slot: SlotNumber, dutyType: DutyType, blockIndexWithinCheckpoint: number, signedByNode: string);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Thrown when attempting to sign data that conflicts with an already-signed duty.
|
|
20
|
+
* This means the same validator tried to sign DIFFERENT data for the same slot.
|
|
21
|
+
*
|
|
22
|
+
* This is expected in HA setups where nodes may build different blocks
|
|
23
|
+
* (e.g., different transaction ordering) - the protection prevents double-signing.
|
|
24
|
+
*/
|
|
25
|
+
export declare class SlashingProtectionError extends Error {
|
|
26
|
+
readonly slot: SlotNumber;
|
|
27
|
+
readonly dutyType: DutyType;
|
|
28
|
+
readonly blockIndexWithinCheckpoint: number;
|
|
29
|
+
readonly existingMessageHash: string;
|
|
30
|
+
readonly attemptedMessageHash: string;
|
|
31
|
+
readonly signedByNode: string;
|
|
32
|
+
constructor(slot: SlotNumber, dutyType: DutyType, blockIndexWithinCheckpoint: number, existingMessageHash: string, attemptedMessageHash: string, signedByNode: string);
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXJyb3JzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZXJyb3JzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFFbEUsT0FBTyxLQUFLLEVBQUUsUUFBUSxFQUFFLE1BQU0sZUFBZSxDQUFDO0FBRTlDOzs7O0dBSUc7QUFDSCxxQkFBYSxzQkFBdUIsU0FBUSxLQUFLO2FBRTdCLElBQUksRUFBRSxVQUFVO2FBQ2hCLFFBQVEsRUFBRSxRQUFRO2FBQ2xCLDBCQUEwQixFQUFFLE1BQU07YUFDbEMsWUFBWSxFQUFFLE1BQU07SUFKdEMsWUFDa0IsSUFBSSxFQUFFLFVBQVUsRUFDaEIsUUFBUSxFQUFFLFFBQVEsRUFDbEIsMEJBQTBCLEVBQUUsTUFBTSxFQUNsQyxZQUFZLEVBQUUsTUFBTSxFQUlyQztDQUNGO0FBRUQ7Ozs7OztHQU1HO0FBQ0gscUJBQWEsdUJBQXdCLFNBQVEsS0FBSzthQUU5QixJQUFJLEVBQUUsVUFBVTthQUNoQixRQUFRLEVBQUUsUUFBUTthQUNsQiwwQkFBMEIsRUFBRSxNQUFNO2FBQ2xDLG1CQUFtQixFQUFFLE1BQU07YUFDM0Isb0JBQW9CLEVBQUUsTUFBTTthQUM1QixZQUFZLEVBQUUsTUFBTTtJQU50QyxZQUNrQixJQUFJLEVBQUUsVUFBVSxFQUNoQixRQUFRLEVBQUUsUUFBUSxFQUNsQiwwQkFBMEIsRUFBRSxNQUFNLEVBQ2xDLG1CQUFtQixFQUFFLE1BQU0sRUFDM0Isb0JBQW9CLEVBQUUsTUFBTSxFQUM1QixZQUFZLEVBQUUsTUFBTSxFQU9yQztDQUNGIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;GAIG;AACH,qBAAa,sBAAuB,SAAQ,KAAK;aAE7B,IAAI,EAAE,UAAU;aAChB,QAAQ,EAAE,QAAQ;aAClB,0BAA0B,EAAE,MAAM;aAClC,YAAY,EAAE,MAAM;IAJtC,YACkB,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,0BAA0B,EAAE,MAAM,EAClC,YAAY,EAAE,MAAM,EAIrC;CACF;AAED;;;;;;GAMG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;aAE9B,IAAI,EAAE,UAAU;aAChB,QAAQ,EAAE,QAAQ;aAClB,0BAA0B,EAAE,MAAM;aAClC,mBAAmB,EAAE,MAAM;aAC3B,oBAAoB,EAAE,MAAM;aAC5B,YAAY,EAAE,MAAM;IANtC,YACkB,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,0BAA0B,EAAE,MAAM,EAClC,mBAAmB,EAAE,MAAM,EAC3B,oBAAoB,EAAE,MAAM,EAC5B,YAAY,EAAE,MAAM,EAOrC;CACF"}
|
package/dest/errors.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom errors for the validator HA signer
|
|
3
|
+
*/ /**
|
|
4
|
+
* Thrown when a duty has already been signed (by any node).
|
|
5
|
+
* This is expected behavior in an HA setup - all nodes try to sign,
|
|
6
|
+
* the first one wins, and subsequent attempts get this error.
|
|
7
|
+
*/ export class DutyAlreadySignedError extends Error {
|
|
8
|
+
slot;
|
|
9
|
+
dutyType;
|
|
10
|
+
blockIndexWithinCheckpoint;
|
|
11
|
+
signedByNode;
|
|
12
|
+
constructor(slot, dutyType, blockIndexWithinCheckpoint, signedByNode){
|
|
13
|
+
super(`Duty ${dutyType} for slot ${slot} already signed by node ${signedByNode}`), this.slot = slot, this.dutyType = dutyType, this.blockIndexWithinCheckpoint = blockIndexWithinCheckpoint, this.signedByNode = signedByNode;
|
|
14
|
+
this.name = 'DutyAlreadySignedError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Thrown when attempting to sign data that conflicts with an already-signed duty.
|
|
19
|
+
* This means the same validator tried to sign DIFFERENT data for the same slot.
|
|
20
|
+
*
|
|
21
|
+
* This is expected in HA setups where nodes may build different blocks
|
|
22
|
+
* (e.g., different transaction ordering) - the protection prevents double-signing.
|
|
23
|
+
*/ export class SlashingProtectionError extends Error {
|
|
24
|
+
slot;
|
|
25
|
+
dutyType;
|
|
26
|
+
blockIndexWithinCheckpoint;
|
|
27
|
+
existingMessageHash;
|
|
28
|
+
attemptedMessageHash;
|
|
29
|
+
signedByNode;
|
|
30
|
+
constructor(slot, dutyType, blockIndexWithinCheckpoint, existingMessageHash, attemptedMessageHash, signedByNode){
|
|
31
|
+
super(`Slashing protection: ${dutyType} for slot ${slot} was already signed with different data. ` + `Existing: ${existingMessageHash.slice(0, 10)}..., Attempted: ${attemptedMessageHash.slice(0, 10)}...`), this.slot = slot, this.dutyType = dutyType, this.blockIndexWithinCheckpoint = blockIndexWithinCheckpoint, this.existingMessageHash = existingMessageHash, this.attemptedMessageHash = attemptedMessageHash, this.signedByNode = signedByNode;
|
|
32
|
+
this.name = 'SlashingProtectionError';
|
|
33
|
+
}
|
|
34
|
+
}
|