GeezerVison is showing github.com — fix(challenge): defer relay disable until rescue proves chunk custody · CortexLM/hypertrain@db94ff4
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: docs/network-v2.md +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -251,6 +251,11 @@ an exact complete receipt or full original chunk coverage, authenticated healthy | 251 | 251 | witnesses and original grant/assignment lineage. The master fetches/hash-checks | 252 | 252 | original bytes before acceptance. Rescue preserves custody without backdating | 253 | 253 | late acceptance or charging the miner for proven storage failure. | | 254 | +Multi-chunk custody without a receipt disables the relay key at once only when | | 255 | +the master holds the original and it proves every chunk hash. Otherwise the key | | 256 | +is disabled only after rescued bytes hash to `delta_hash`: the grant could not | | 257 | +tie those chunk hashes to the commit, so a mismatch answers 422 and leaves the | | 258 | +relay enabled. | 254 | 259 | Retention release waits for finality, disputes and vesting; signed extensions, | 255 | 260 | overlapping old/new keys and drain preserve existing retrieval authority. | 256 | 261 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/hypertrain/challenge/store.py +31-14Lines changed: 31 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5537,15 +5537,20 @@ def relay_failure_v2(self, run_id: str, raw: bytes) -> dict[str, Any]: | 5537 | 5537 | ) | 5538 | 5538 | if set(evidence.chunk_ack_hashes) != {c.digest() for c in chunks}: | 5539 | 5539 | raise ChallengeError(422, "failure includes unproved chunk custody") | 5540 | | - if receipt is None: | 5541 | | - # upload_grant_v2 cannot tie chunk hashes to delta_hash before the master holds | 5542 | | - # the original, so chunk-only custody may cover bytes no receipt could follow. | | 5540 | + # upload_grant_v2 cannot tie a multi-chunk list to delta_hash before the master | | 5541 | + # holds the original, so a miner could frame an honest relay with chunk hashes no | | 5542 | + # receipt could follow. Unless the held original proves each chunk, such a key is | | 5543 | + # disabled only once rescue hashes to delta_hash. | | 5544 | + deferred = receipt is None and len(chunk_manifest.chunks) > 1 | | 5545 | + if deferred: | 5543 | 5546 | try: | 5544 | | - self.objects.get(grant.delta_hash) | | 5547 | + original = self.objects.get(grant.delta_hash) | | 5548 | + deferred = any( | | 5549 | + sha256_hex(original[c.off : c.off + c.len]) != c.chunk_sha256 | | 5550 | + for c in chunk_manifest.chunks | | 5551 | + ) | 5545 | 5552 | except ObjectNotFound: | 5546 | | - raise ChallengeError( | 5547 | | - 409, "chunk-only custody without the original; rescue first" | 5548 | | - ) from None | | 5553 | + pass | 5549 | 5554 | if min(evidence.observed_beacons) <= opportunity.service_deadline or max( | 5550 | 5555 | evidence.observed_beacons | 5551 | 5556 | ) > self._now(self._db): | @@ -5584,13 +5589,14 @@ def relay_failure_v2(self, run_id: str, raw: bytes) -> dict[str, Any]: | 5584 | 5589 | evidence.digest(), | 5585 | 5590 | {"artifacts": data["artifacts"], "observations": data["observations"]}, | 5586 | 5591 | ) | 5587 | | - self._put_record_v2( | 5588 | | - run_id, | 5589 | | - "relay-disabled", | 5590 | | - f"{grant.relay_id}:{grant.assignment_epoch}:{evidence.key_id}", | 5591 | | - {"key_id": evidence.key_id, "evidence_hash": evidence.digest()}, | 5592 | | - ) | 5593 | | - result = {"result": evidence.result, "disabled": True, "miner_penalty": 0} | | 5592 | + if not deferred: | | 5593 | + self._put_record_v2( | | 5594 | + run_id, | | 5595 | + "relay-disabled", | | 5596 | + f"{grant.relay_id}:{grant.assignment_epoch}:{evidence.key_id}", | | 5597 | + {"key_id": evidence.key_id, "evidence_hash": evidence.digest()}, | | 5598 | + ) | | 5599 | + result = {"result": evidence.result, "disabled": not deferred, "miner_penalty": 0} | 5594 | 5600 | self._notify_processes_v2() | 5595 | 5601 | return result | 5596 | 5602 |
| @@ -5782,6 +5788,17 @@ async def relay_fallback_v2( | 5782 | 5788 | if len(recovered) != grant.size or sha256_hex(bytes(recovered)) != grant.delta_hash: | 5783 | 5789 | raise ChallengeError(422, "rescued original bytes differ from signed commit") | 5784 | 5790 | self.objects.put(bytes(recovered)) | | 5791 | + disabled = f"{grant.relay_id}:{grant.assignment_epoch}:{failure.key_id}" | | 5792 | + if not self._db.execute( # Deferred by relay_failure_v2; earlier evidence wins. | | 5793 | + "SELECT 1 FROM records_v2 WHERE run_id=? AND kind='relay-disabled' AND id=?", | | 5794 | + (run_id, disabled), | | 5795 | + ).fetchone(): | | 5796 | + self._put_record_v2( | | 5797 | + run_id, | | 5798 | + "relay-disabled", | | 5799 | + disabled, | | 5800 | + {"key_id": failure.key_id, "evidence_hash": failure_hash}, | | 5801 | + ) | 5785 | 5802 | for request_raw, response in responses: | 5786 | 5803 | self._put_record_v2( | 5787 | 5804 | run_id, | |
Collapse file Copy file name to clipboardExpand all lines: tests/challenge/test_service_network_v2.py +53-19Lines changed: 53 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3454,7 +3454,18 @@ async def drain(): | 3454 | 3454 |
| 3455 | 3455 |
| 3456 | 3456 | @pytest.mark.parametrize( | 3457 | | - "invalid", [None, "partial-receipt", "duplicate", "gap", "contradiction", "chunk-only"] | | 3457 | + "invalid", | | 3458 | + [ | | 3459 | + None, | | 3460 | + "partial-receipt", | | 3461 | + "duplicate", | | 3462 | + "gap", | | 3463 | + "contradiction", | | 3464 | + "chunk-only", | | 3465 | + "forged", | | 3466 | + "held-chunk-only", | | 3467 | + "held-forged", | | 3468 | + ], | 3458 | 3469 | ) | 3459 | 3470 | def test_complete_receipt_prefix_rescue_http(network, monkeypatch, tmp_path, invalid): | 3460 | 3471 | """Exact two-chunk receipt/prefix regression; no live-round/graduation sweep.""" | @@ -3463,7 +3474,7 @@ def test_complete_receipt_prefix_rescue_http(network, monkeypatch, tmp_path, inv | 3463 | 3474 | import anyio | 3464 | 3475 | import httpx | 3465 | 3476 |
| 3466 | | - from hypertrain.data.store import LocalFSStore | | 3477 | + from hypertrain.data.store import CorruptObjectError, LocalFSStore | 3467 | 3478 | from hypertrain.data.stream_store import StreamStore | 3468 | 3479 | from hypertrain.protocol import envelope_v2, relay_envelope | 3469 | 3480 | from hypertrain.protocol.relay_messages import ( | @@ -3502,11 +3513,16 @@ def test_complete_receipt_prefix_rescue_http(network, monkeypatch, tmp_path, inv | 3502 | 3513 | ) | 3503 | 3514 | assert response.status_code == 200, response.text | 3504 | 3515 | payload = b"a" * (4 << 20) + b"b" | | 3516 | + # forged: miner-chosen chunk hashes that do not concatenate to delta_hash; an | | 3517 | + # unheld delta lets upload_grant_v2 sign such a manifest. | | 3518 | + forged = invalid in {"forged", "held-forged"} | | 3519 | + sent = b"x" * (4 << 20) + b"y" if forged else payload | | 3520 | + chunk_only = invalid in {"chunk-only", "forged", "held-chunk-only", "held-forged"} | 3505 | 3521 | chunks = UploadChunkManifest( | 3506 | 3522 | size=len(payload), | 3507 | 3523 | chunks=[ | 3508 | | - UploadChunk(index=0, off=0, len=4 << 20, chunk_sha256=sha256_hex(payload[:-1])), | 3509 | | - UploadChunk(index=1, off=4 << 20, len=1, chunk_sha256=sha256_hex(payload[-1:])), | | 3524 | + UploadChunk(index=0, off=0, len=4 << 20, chunk_sha256=sha256_hex(sent[:-1])), | | 3525 | + UploadChunk(index=1, off=4 << 20, len=1, chunk_sha256=sha256_hex(sent[-1:])), | 3510 | 3526 | ], | 3511 | 3527 | ) | 3512 | 3528 | net = RelayNetworkManifest( | @@ -3614,14 +3630,20 @@ async def uploaded(): | 3614 | 3630 | async def source(part): | 3615 | 3631 | yield part | 3616 | 3632 |
| 3617 | | - first = await relay.chunk(grant.digest(), 0, source(payload[:-1])) | 3618 | | - last = await relay.chunk(grant.digest(), 1, source(payload[-1:])) | | 3633 | + first = await relay.chunk(grant.digest(), 0, source(sent[:-1])) | | 3634 | + if forged: | | 3635 | + # The relay signs both chunk acks, then cannot complete bytes missing delta_hash. | | 3636 | + with pytest.raises(CorruptObjectError): | | 3637 | + await relay.chunk(grant.digest(), 1, source(sent[-1:])) | | 3638 | + last = (await relay.state()).uploads[grant.digest()].chunks["1"] | | 3639 | + return opportunity, first, last, last | | 3640 | + last = await relay.chunk(grant.digest(), 1, source(sent[-1:])) | 3619 | 3641 | return opportunity, first, last, await relay.complete(grant.digest()) | 3620 | 3642 |
| 3621 | 3643 | opportunity_env, prefix_env, last_env, receipt_env = anyio.run(uploaded) | 3622 | 3644 | opportunity = AcceptedUploadAck.model_validate(opportunity_env.body) | 3623 | 3645 | prefix = ChunkCustodyAck.model_validate(prefix_env.body) | 3624 | | - receipt = RelayReceipt.model_validate(receipt_env.body) | | 3646 | + receipt = None if forged else RelayReceipt.model_validate(receipt_env.body) | 3625 | 3647 | if invalid == "partial-receipt": | 3626 | 3648 | receipt = receipt.model_copy(update={"size": prefix.len, "delta_hash": prefix.chunk_sha256}) | 3627 | 3649 | receipt_env = relay_envelope.parse_envelope( | @@ -3642,11 +3664,13 @@ async def source(part): | 3642 | 3664 | if invalid == "duplicate": | 3643 | 3665 | artifacts.insert(2, prefix_env.model_dump(mode="json")) | 3644 | 3666 | # Chunk-only custody while the master never held the original: the grant could not | 3645 | | - # check that the chunk hashes concatenate to delta_hash, so no key is disabled. | | 3667 | + # check that the chunk hashes concatenate to delta_hash, so disabling waits for rescue. | 3646 | 3668 | acks = [prefix] | 3647 | | - if invalid == "chunk-only": | | 3669 | + if chunk_only: | 3648 | 3670 | artifacts[2] = last_env.model_dump(mode="json") | 3649 | 3671 | acks.append(ChunkCustodyAck.model_validate(last_env.body)) | | 3672 | + if invalid in {"held-chunk-only", "held-forged"}: | | 3673 | + network.store.objects.put(payload) | 3650 | 3674 | network.push(grant.exp_drand + 1) | 3651 | 3675 | for auditor in AUDITORS: | 3652 | 3676 | url = network.url + f"/relay-observers/{grant.digest()}/{auditor.ss58}" | @@ -3665,9 +3689,9 @@ async def source(part): | 3665 | 3689 | upload_ack_hash=opportunity.digest(), | 3666 | 3690 | chunk_manifest_hash=grant.chunk_manifest_hash, | 3667 | 3691 | chunk_ack_hashes=[a.digest() for a in acks], | 3668 | | - receipt_hash=None if invalid == "chunk-only" else receipt.digest(), | | 3692 | + receipt_hash=None if chunk_only else receipt.digest(), | 3669 | 3693 | retention_hash=sha256_hex(canonicalize(sorted(a.digest() for a in acks))) | 3670 | | - if invalid == "chunk-only" | | 3694 | + if chunk_only | 3671 | 3695 | else receipt.digest(), | 3672 | 3696 | request=None, | 3673 | 3697 | response=None, | @@ -3698,15 +3722,19 @@ async def source(part): | 3698 | 3722 | assert failure.status_code == 422, failure.text | 3699 | 3723 | anyio.run(backing.aclose) | 3700 | 3724 | return | 3701 | | - if invalid == "chunk-only": | 3702 | | - assert failure.status_code == 409, failure.text | 3703 | | - assert "rescue" in failure.json()["detail"] | 3704 | | - assert network.store._db.execute( | 3705 | | - "SELECT COUNT(*) FROM records_v2 WHERE kind IN ('relay-disabled','relay-failure')" | 3706 | | - ).fetchone() == (0,) | | 3725 | + assert failure.status_code == 200, failure.text | | 3726 | + | | 3727 | + def disabled(): | | 3728 | + return network.store._db.execute( | | 3729 | + "SELECT COUNT(*) FROM records_v2 WHERE kind='relay-disabled'" | | 3730 | + ).fetchone()[0] | | 3731 | + | | 3732 | + if invalid == "held-chunk-only": # The held original proves each chunk: disable now. | | 3733 | + assert failure.json()["disabled"] and disabled() == 1 | 3707 | 3734 | anyio.run(backing.aclose) | 3708 | 3735 | return | 3709 | | - assert failure.status_code == 200, failure.text | | 3736 | + if chunk_only: | | 3737 | + assert not failure.json()["disabled"] and disabled() == 0 | 3710 | 3738 | app = relay_app(relay, "x" * 32) | 3711 | 3739 | monkeypatch.setattr( | 3712 | 3740 | httpx, | @@ -3716,8 +3744,14 @@ async def source(part): | 3716 | 3744 | rescued = network.client.post( | 3717 | 3745 | network.url + f"/admin/relay-fallback/{evidence.digest()}", headers=admin() | 3718 | 3746 | ) | 3719 | | - if invalid is not None: | | 3747 | + if invalid == "chunk-only": | | 3748 | + assert rescued.status_code == 200, rescued.text | | 3749 | + assert network.store.objects.get(grant.delta_hash) == payload | | 3750 | + assert disabled() == 1 | | 3751 | + elif invalid is not None: | 3720 | 3752 | assert rescued.status_code == 422, rescued.text | | 3753 | + if forged: | | 3754 | + assert disabled() == 0 | 3721 | 3755 | else: | 3722 | 3756 | assert rescued.status_code == 200, rescued.text | 3723 | 3757 | assert rescued.json()["grant"] == signed_grant | |
|