hosted_room_work_lineage.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Validate passive evidence against committed authority spans, never elect an owner."""
  2. from gateway.hosted_room_authority_history import TRANSFER_KINDS, at_sequence, read_history_locked, validate_history
  3. def source_prefix_locked(conn, room_id, authority, seq):
  4. from gateway.hosted_room_work_records import WorkRecordPrefixError, digest
  5. history = read_history_locked(conn, room_id, gateway_id=authority["gateway_id"], epoch=authority["epoch"])
  6. if history is None:
  7. raise WorkRecordPrefixError("successor work evidence has no verified lineage")
  8. spans = validate_history(history, gateway_id=authority["gateway_id"], epoch=authority["epoch"])
  9. count = 0
  10. import json
  11. for event in conn.execute("SELECT seq,authority_epoch,actor_json,kind FROM hosted_room_events WHERE room_id=? AND seq<=? ORDER BY seq", (room_id, seq)):
  12. count += 1
  13. span = at_sequence(spans, count)
  14. actor = json.loads(event["actor_json"])
  15. boundary = span.epoch > 1 and count == span.from_seq
  16. if (event["seq"] != count or event["authority_epoch"] != span.epoch
  17. or (actor.get("kind") == "gateway" and actor.get("id") != span.gateway_id)
  18. or (event["kind"] in TRANSFER_KINDS) != boundary):
  19. raise WorkRecordPrefixError("work record history prefix is not verified")
  20. if count != seq:
  21. raise WorkRecordPrefixError("work record history prefix is unavailable")
  22. return spans, digest(history)
  23. def validate_provenance(record, spans):
  24. from gateway.hosted_room_work_records import WorkRecordError
  25. # Execution generation remains an independent per-task counter.
  26. for task in record["tasks"]:
  27. at_sequence(spans, task["source_event_seq"])
  28. for receipt in record["receipts"]:
  29. if not any(s.gateway_id == receipt["home_install_id"] == receipt["authority_gateway_id"]
  30. and s.epoch == receipt["authority_epoch"] for s in spans):
  31. raise WorkRecordError("work record receipt has no original authority span")
  32. def task_origins(record, spans):
  33. return {task["task_id"]: {"gateway_id": origin.gateway_id, "epoch": origin.epoch}
  34. for task in record["tasks"] for origin in [at_sequence(spans, task["source_event_seq"])]}