GeezerVison is showing github.com — feat(validator): add an operator-only watermark reset command · CortexLM/cortex@6263a2a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | 23 | 23 | each paying `share * min(sum(leaves), 10^12) / 10^12`, remainder burned. | 24 | 24 | Validators need no change beyond the release; they recompute signed leaves. | 25 | 25 | - `GET /v1/metagraph/latest` serves the sealed hotkey map to challenges. | | 26 | +- `cortex validator-watermark-reset`: operator-only, local repair that lowers a | | 27 | + wrongly advanced `challenges_version`, `measurements_version` or `epoch` | | 28 | + watermark (requires `--confirm`, never raises one) and audits it in | | 29 | + `consensus_watermark_resets`. | 26 | 30 |
| 27 | 31 | ### Changed | 28 | 32 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/external-miner/troubleshoot.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -105,6 +105,7 @@ catalog; follow the documentation and checklist in the current signed topic. | 105 | 105 | | Missing/invalid peer sample | Configure independently selected HTTPS peers with metagraph `validator_permit`; do not disable verification | | 106 | 106 | | Conflicting signed roots or equivocation | Preserve the journal and signed evidence; reconcile the network state | | 107 | 107 | | Stale block, future epoch, reorg or changed latest | Wait for a current valid seal; do not force-submit the old one | | | 108 | +| `consensus rollback refused` | The gateway serves a lower `challenges_version`, `measurements_version` or epoch than this validator already accepted. Ask the master operator first; only if the higher value was advanced wrongly, lower it with `cortex validator-watermark-reset` (see [the validator guide](validators.md#repairing-a-consensus-watermark)). Do not delete the database | | 108 | 109 | | Ambiguous chain dispatch remains pending | Reconcile inclusion/finalization before retrying; retain the SQLite journal | | 109 | 110 | | CRv4 or drand failure | Do not fall back to public weights while chain commit-reveal is enabled | | 110 | 111 | | `/v1/attest/*` returns `503` and `verified: false` | DCAP verification is not implemented in the Python validator | | |
Collapse file Copy file name to clipboardExpand all lines: docs/external-miner/validators.md +25Lines changed: 25 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -234,6 +234,31 @@ Only `not_broadcast` releases the epoch for a retry. Keep the underlying chain | 234 | 234 | receipt or nonce query private; the journal stores its digest. Do not delete the | 235 | 235 | database to force a retry. | 236 | 236 |
| | 237 | +### Repairing a consensus watermark | | 238 | + | | 239 | +The journal keeps rollback watermarks for `challenges_version`, | | 240 | +`measurements_version` and `epoch`; a lower served value fails with | | 241 | +`consensus rollback refused`. If a watermark was advanced wrongly (for example a | | 242 | +profile version published by mistake and then withdrawn), stop the validator and | | 243 | +lower it locally. The command touches only the state database: no network, no | | 244 | +wallet. | | 245 | + | | 246 | +```bash | | 247 | +cortex validator-watermark-reset \ | | 248 | + --state-db /var/lib/base/validator.sqlite3 \ | | 249 | + --name challenges_version --to 2 --confirm | | 250 | +``` | | 251 | + | | 252 | +It refuses without `--confirm`, refuses a name outside those three, a negative | | 253 | +value, a watermark that was never set and any value above the current one (it | | 254 | +only lowers or repairs). It prints the old and new values and appends an audit | | 255 | +row to `consensus_watermark_resets(name, old_value, new_value, at)` in the same | | 256 | +transaction. | | 257 | + | | 258 | +**Warning:** resetting below a version or epoch this validator really accepted | | 259 | +re-opens rollback to that value. Confirm the correct value with the master | | 260 | +operator first. | | 261 | + | 237 | 262 | The peer listener exposes: | 238 | 263 |
| 239 | 264 | | Route | Response | | |
Collapse file Copy file name to clipboardExpand all lines: src/cortex/cli.py +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | from cortex.errors import ServiceError | 17 | 17 | from cortex.http import read_private_file | 18 | 18 | from cortex.miner import MinerClient, load_seed | | 19 | +from cortex.protocol import ProtocolError | 19 | 20 | from cortex.protocol.crypto import decode_hotkey | | 21 | +from cortex.validator.evidence import RESETTABLE_WATERMARKS | 20 | 22 | from cortex.wallet import HotkeySigner, load_wallet_hotkey | 21 | 23 |
| 22 | 24 |
| @@ -40,6 +42,14 @@ def parser() -> argparse.ArgumentParser: | 40 | 42 | reconcile.add_argument("--attempt-id", required=True) | 41 | 43 | reconcile.add_argument("--result", choices=("submitted", "not_broadcast"), required=True) | 42 | 44 | reconcile.add_argument("--evidence-digest", required=True) | | 45 | + reset = commands.add_parser( | | 46 | + "validator-watermark-reset", | | 47 | + help="lower one wrongly advanced consensus watermark (local state only)", | | 48 | + ) | | 49 | + reset.add_argument("--state-db", type=Path, required=True) | | 50 | + reset.add_argument("--name", choices=RESETTABLE_WATERMARKS, required=True) | | 51 | + reset.add_argument("--to", type=int, required=True) | | 52 | + reset.add_argument("--confirm", action="store_true") | 43 | 53 | vm = commands.add_parser("vm-host", help="serve the dedicated Firecracker host") | 44 | 54 | vm.add_argument("--config", type=Path, required=True) | 45 | 55 | vm.add_argument( | @@ -230,6 +240,24 @@ def run_validator_reconcile(args: argparse.Namespace) -> None: | 230 | 240 | print(json.dumps(result, indent=2, sort_keys=True)) | 231 | 241 |
| 232 | 242 |
| | 243 | +def run_validator_watermark_reset(args: argparse.Namespace) -> None: | | 244 | + from cortex.validator import SubmissionJournal | | 245 | + | | 246 | + # Refusal reasons are fixed strings with no credential, so show them to the operator. | | 247 | + if not args.confirm: | | 248 | + raise SystemExit("Cortex command failed: --confirm required; lowering re-opens rollback") | | 249 | + if not args.state_db.is_file(): | | 250 | + raise SystemExit("Cortex command failed: validator state database not found") | | 251 | + journal = SubmissionJournal(args.state_db) | | 252 | + try: | | 253 | + old = journal.evidence.reset_watermark(args.name, args.to) | | 254 | + except ProtocolError as error: | | 255 | + raise SystemExit(f"Cortex command failed: {error}") from None | | 256 | + finally: | | 257 | + journal.close() | | 258 | + print(json.dumps({"name": args.name, "old_value": old, "new_value": args.to}, indent=2)) | | 259 | + | | 260 | + | 233 | 261 | def main(argv: list[str] | None = None) -> None: | 234 | 262 | values = sys.argv[1:] if argv is None else argv | 235 | 263 | if values and values[0] == "validator": | @@ -259,6 +287,8 @@ def main(argv: list[str] | None = None) -> None: | 259 | 287 | serve(args.config) | 260 | 288 | elif args.command == "validator-reconcile": | 261 | 289 | run_validator_reconcile(args) | | 290 | + elif args.command == "validator-watermark-reset": | | 291 | + run_validator_watermark_reset(args) | 262 | 292 | else: | 263 | 293 | action = {"master": run_master, "miner": run_miner, "topic-create": run_topic_create} | 264 | 294 | asyncio.run(action[args.command](args)) | |
Collapse file Copy file name to clipboardExpand all lines: src/cortex/validator/evidence.py +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | from cortex.protocol import Bundle, ProtocolError | 10 | 10 | from cortex.protocol.consensus import Dissent, RootStatement | 11 | 11 |
| | 12 | +RESETTABLE_WATERMARKS = ("challenges_version", "measurements_version", "epoch") | | 13 | + | 12 | 14 |
| 13 | 15 | class EvidenceStore: | 14 | 16 | def __init__(self, connection: sqlite3.Connection): | @@ -29,6 +31,10 @@ def __init__(self, connection: sqlite3.Connection): | 29 | 31 | CREATE TABLE IF NOT EXISTS consensus_watermarks ( | 30 | 32 | name TEXT PRIMARY KEY, value TEXT NOT NULL | 31 | 33 | ); | | 34 | + CREATE TABLE IF NOT EXISTS consensus_watermark_resets ( | | 35 | + name TEXT NOT NULL, old_value TEXT NOT NULL, new_value TEXT NOT NULL, | | 36 | + at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')) | | 37 | + ); | 32 | 38 | """) | 33 | 39 |
| 34 | 40 | def check_watermark(self, name: str, value: int) -> None: | @@ -46,6 +52,35 @@ def watermark(self, name: str, value: int) -> None: | 46 | 52 | (name, str(value)), | 47 | 53 | ) | 48 | 54 |
| | 55 | + def reset_watermark(self, name: str, value: int) -> int: | | 56 | + """Operator repair: lower one rollback watermark and audit it; never raises one.""" | | 57 | + if name not in RESETTABLE_WATERMARKS: | | 58 | + raise ProtocolError("unknown consensus watermark") | | 59 | + if type(value) is not int or value < 0: | | 60 | + raise ProtocolError("watermark must be a non-negative integer") | | 61 | + self.connection.execute("BEGIN IMMEDIATE") | | 62 | + try: | | 63 | + row = self.connection.execute( | | 64 | + "SELECT value FROM consensus_watermarks WHERE name=?", (name,) | | 65 | + ).fetchone() | | 66 | + if row is None: | | 67 | + raise ProtocolError("consensus watermark not set") | | 68 | + old = int(row[0]) | | 69 | + if value > old: | | 70 | + raise ProtocolError("watermark reset may only lower a value") | | 71 | + self.connection.execute( | | 72 | + "UPDATE consensus_watermarks SET value=? WHERE name=?", (str(value), name) | | 73 | + ) | | 74 | + self.connection.execute( | | 75 | + "INSERT INTO consensus_watermark_resets (name,old_value,new_value) VALUES (?,?,?)", | | 76 | + (name, str(old), str(value)), | | 77 | + ) | | 78 | + except BaseException: | | 79 | + self.connection.execute("ROLLBACK") | | 80 | + raise | | 81 | + self.connection.execute("COMMIT") | | 82 | + return old | | 83 | + | 49 | 84 | def root(self, statement: RootStatement, *, local: bool = False) -> bool: | 50 | 85 | statement.verify() | 51 | 86 | epoch = f"{statement.epoch:020d}" | |
Collapse file Copy file name to clipboardExpand all lines: tests/validator/test_validator.py +48Lines changed: 48 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import json | 1 | 2 | from dataclasses import replace | 2 | 3 | from hashlib import sha256 | 3 | 4 | from types import SimpleNamespace | @@ -557,6 +558,53 @@ async def test_trust_version_rollback_is_refused_across_restart(network): | 557 | 558 | assert len(network.chain.submissions) == 1 | 558 | 559 |
| 559 | 560 |
| | 561 | +def reset(db_path, *extra): | | 562 | + from cortex.cli import main | | 563 | + | | 564 | + main(["validator-watermark-reset", "--state-db", str(db_path), *extra]) | | 565 | + | | 566 | + | | 567 | +async def test_cli_watermark_reset_lowers_a_wrong_version_and_audits_it(network, capsys): | | 568 | + import sqlite3 | | 569 | + | | 570 | + original = network.validator.trust | | 571 | + network.validator.trust_loader = lambda _: replace(original, challenges_version=2) | | 572 | + await network.validator.run_once() | | 573 | + network.journal.close() | | 574 | + args = ("--name", "challenges_version", "--to", "1") | | 575 | + for refused in ( | | 576 | + args, # no --confirm | | 577 | + ("--name", "challenges_version", "--to", "3", "--confirm"), # raising | | 578 | + ("--name", "netuid", "--to", "1", "--confirm"), | | 579 | + ("--name", "challenges_version", "--to", "-1", "--confirm"), | | 580 | + ): | | 581 | + with pytest.raises(SystemExit): | | 582 | + reset(network.db_path, *refused) | | 583 | + capsys.readouterr() | | 584 | + reset(network.db_path, *args, "--confirm") | | 585 | + assert json.loads(capsys.readouterr().out) == { | | 586 | + "name": "challenges_version", | | 587 | + "old_value": 2, | | 588 | + "new_value": 1, | | 589 | + } | | 590 | + with sqlite3.connect(network.db_path) as connection: | | 591 | + assert connection.execute( | | 592 | + "SELECT name, old_value, new_value FROM consensus_watermark_resets" | | 593 | + ).fetchall() == [("challenges_version", "2", "1")] | | 594 | + network.validator.journal = SubmissionJournal(network.db_path) | | 595 | + network.validator.trust_loader = lambda _: original | | 596 | + assert (await network.validator.run_once()).outcome == "already_submitted_or_pending" | | 597 | + | | 598 | + | | 599 | +def test_cli_watermark_reset_refuses_an_unset_watermark_and_allows_equal(network, capsys): | | 600 | + network.journal.evidence.watermark("epoch", 12) | | 601 | + network.journal.close() | | 602 | + with pytest.raises(SystemExit): | | 603 | + reset(network.db_path, "--name", "measurements_version", "--to", "0", "--confirm") | | 604 | + reset(network.db_path, "--name", "epoch", "--to", "12", "--confirm") | | 605 | + assert json.loads(capsys.readouterr().out)["new_value"] == 12 | | 606 | + | | 607 | + | 560 | 608 | async def test_peer_api_serves_signed_evidence_and_never_claims_dcap_verified(network): | 561 | 609 | from cortex.protocol.consensus import RootStatement | 562 | 610 | from cortex.validator.evidence import peer_app | |
|