GeezerVison is showing github.com — fix(challenge): keep one live upload grant per relay assignment · CortexLM/hypertrain@6e90903
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: src/hypertrain/challenge/store.py +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9800,6 +9800,27 @@ def upload_grant_v2( | 9800 | 9800 | commit = self._record_v2(run_id, "commit", f"{assignment.w}:{env.signer}")["body"] | 9801 | 9801 | if chunk.size != commit["delta_bytes"]: | 9802 | 9802 | raise ChallengeError(422, "grant size differs from accepted commit") | | 9803 | + # One live grant per assignment: a retry gets the stored grant, another chunk | | 9804 | + # list waits for expiry. Without the original, the list is miner-chosen. | | 9805 | + for (stored,) in self._db.execute( | | 9806 | + "SELECT data FROM records_v2 WHERE run_id=? AND kind='grant' " | | 9807 | + "AND json_extract(data,'$.body.w')=? AND json_extract(data,'$.body.hotkey')=? " | | 9808 | + "AND json_extract(data,'$.body.relay_id')=? " | | 9809 | + "AND json_extract(data,'$.body.assignment_epoch')=? " | | 9810 | + "AND json_extract(data,'$.body.exp_drand')>?", | | 9811 | + ( | | 9812 | + run_id, | | 9813 | + assignment.w, | | 9814 | + env.signer, | | 9815 | + assignment.primary_id, | | 9816 | + assignment.assignment_epoch, | | 9817 | + now, | | 9818 | + ), | | 9819 | + ): | | 9820 | + live = json.loads(stored) | | 9821 | + if live["body"]["chunk_manifest_hash"] != chunk.chunk_manifest_hash(): | | 9822 | + raise ChallengeError(409, "live upload grant binds another chunk list") | | 9823 | + return live | 9803 | 9824 | grant = UploadGrant( | 9804 | 9825 | w=assignment.w, | 9805 | 9826 | hotkey=env.signer, | |
Collapse file Copy file name to clipboardExpand all lines: tests/challenge/test_service_network_v2.py +116Lines changed: 116 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4147,3 +4147,119 @@ def wire(request): | 4147 | 4147 | with pytest.raises(CapturedSubject): | 4148 | 4148 | actor.run_round(1) | 4149 | 4149 | assert json.loads(canonicalize(saved["commit"]))["body"]["w"] == 1 | | 4150 | + | | 4151 | + | | 4152 | +def test_upload_grant_one_live_grant_when_original_unheld(network): | | 4153 | + from hypertrain.protocol import relay_envelope | | 4154 | + from hypertrain.protocol.messages_v2 import CommitV2 | | 4155 | + from hypertrain.protocol.relay_messages import ( | | 4156 | + RelayAssignment, | | 4157 | + RelayNetworkManifest, | | 4158 | + UploadChunk, | | 4159 | + UploadChunkManifest, | | 4160 | + ) | | 4161 | + | | 4162 | + assert network.join().status_code == 200 | | 4163 | + run_id = network.manifest.run_id() | | 4164 | + registry = network.store._registry_v2(network.manifest) | | 4165 | + relay_net = RelayNetworkManifest( | | 4166 | + run_id=run_id, | | 4167 | + base_manifest_hash=network.manifest.training.training_hash(), | | 4168 | + registry_hash=registry.digest(), | | 4169 | + specs_hash=sha256_hex(canonicalize([s.body() for s in registry.specs])), | | 4170 | + assignment_policy="master-observed-median3-v1", | | 4171 | + ) | | 4172 | + payload = b"a" * (4 << 20) + b"b" | | 4173 | + assignment = RelayAssignment( | | 4174 | + w=0, | | 4175 | + hotkey=HOT[0].ss58, | | 4176 | + primary_id="local", | | 4177 | + fallback_ids=[], | | 4178 | + assignment_epoch=0, | | 4179 | + manifest_hash=relay_net.digest(), | | 4180 | + exp_drand=552, | | 4181 | + ) | | 4182 | + commit = CommitV2.model_validate( | | 4183 | + { | | 4184 | + "w": 0, | | 4185 | + "hotkey": HOT[0].ss58, | | 4186 | + "leaf_scheme": "ht-leaf-v1", | | 4187 | + "n_leaves": 2, | | 4188 | + "leaves_root": "01" * 32, | | 4189 | + "metrics_root": "02" * 32, | | 4190 | + "final_theta_hash": "03" * 32, | | 4191 | + "tokens": 8, | | 4192 | + "ef_in_hash": "04" * 32, | | 4193 | + "ef_out_hash": "05" * 32, | | 4194 | + "delta_hash": sha256_hex(payload), | | 4195 | + "delta_bytes": len(payload), | | 4196 | + } | | 4197 | + ) | | 4198 | + with network.store._tx(): | | 4199 | + network.store._put_record_v2( | | 4200 | + run_id, | | 4201 | + "relay-assignment", | | 4202 | + f"0:{HOT[0].ss58}", | | 4203 | + relay_envelope.seal(COORD, "RelayAssignment", run_id, assignment, 552), | | 4204 | + ) | | 4205 | + network.store._put_record_v2( | | 4206 | + run_id, "commit", f"0:{HOT[0].ss58}", network.signed(HOT[0], "CommitV2", commit) | | 4207 | + ) | | 4208 | + network.push(385) | | 4209 | + | | 4210 | + def manifest(data): | | 4211 | + return UploadChunkManifest( | | 4212 | + size=len(data), | | 4213 | + chunks=[ | | 4214 | + UploadChunk(index=0, off=0, len=4 << 20, chunk_sha256=sha256_hex(data[:-1])), | | 4215 | + UploadChunk(index=1, off=4 << 20, len=1, chunk_sha256=sha256_hex(data[-1:])), | | 4216 | + ], | | 4217 | + ) | | 4218 | + | | 4219 | + def request(chunks, expiry=583): | | 4220 | + return network.client.post( | | 4221 | + network.url + "/upload-grant", | | 4222 | + json=relay_envelope.seal(HOT[0], "UploadChunkManifest", run_id, chunks, expiry), | | 4223 | + ) | | 4224 | + | | 4225 | + def grants(): | | 4226 | + return network.store._db.execute( | | 4227 | + "SELECT COUNT(*) FROM records_v2 WHERE kind='grant'" | | 4228 | + ).fetchone()[0] | | 4229 | + | | 4230 | + # An expired grant for another list (e.g. a past attempt) never blocks a new one. | | 4231 | + stale = manifest(b"x" * (4 << 20) + b"y") | | 4232 | + with network.store._tx(): | | 4233 | + network.store._put_record_v2( | | 4234 | + run_id, | | 4235 | + "grant", | | 4236 | + "ee" * 32, | | 4237 | + { | | 4238 | + "body": { | | 4239 | + "w": 0, | | 4240 | + "hotkey": HOT[0].ss58, | | 4241 | + "relay_id": "local", | | 4242 | + "assignment_epoch": 0, | | 4243 | + "chunk_manifest_hash": stale.chunk_manifest_hash(), | | 4244 | + "exp_drand": 385, | | 4245 | + } | | 4246 | + }, | | 4247 | + ) | | 4248 | + first = request(manifest(payload)) | | 4249 | + assert first.status_code == 200, first.text | | 4250 | + # Honest retry (fresh miner signature, same chunk list): same signed grant, no new row. | | 4251 | + retry = request(manifest(payload), expiry=584) | | 4252 | + assert retry.status_code == 200, retry.text | | 4253 | + assert retry.json() == first.json() and grants() == 2 | | 4254 | + forged = request(stale) | | 4255 | + assert forged.status_code == 409, forged.text | | 4256 | + assert "another chunk list" in forged.text and grants() == 2 | | 4257 | + # Concurrent retries serialize on the store transaction: one grant for both. | | 4258 | + from concurrent.futures import ThreadPoolExecutor | | 4259 | + | | 4260 | + raw = canonicalize( | | 4261 | + relay_envelope.seal(HOT[0], "UploadChunkManifest", run_id, manifest(payload), 590) | | 4262 | + ) | | 4263 | + with ThreadPoolExecutor(2) as pool: | | 4264 | + both = list(pool.map(lambda _: network.store.upload_grant_v2(run_id, raw), range(2))) | | 4265 | + assert both == [first.json(), first.json()] and grants() == 2 |
|