65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
"""Feasibility witness generator.
|
|
|
|
Produces a compact, quickly-verifiable witness for a PlanDelta describing
|
|
coarse resource bounds (fuel, power, payload size). The witness is intended
|
|
to be inexpensive to compute and small to attach to custody decisions.
|
|
"""
|
|
from typing import Dict, Any
|
|
from .plan_delta import PlanDelta
|
|
import json
|
|
|
|
|
|
def generate_feasibility_witness(delta: PlanDelta) -> Dict[str, Any]:
|
|
"""Produce a compact witness with conservative bounds.
|
|
|
|
The witness includes:
|
|
- total_fuel: sum of declared fuel in commitments
|
|
- total_power: sum of declared power in commitments
|
|
- max_payload_bytes: sum of payload_bytes fields (upper bound)
|
|
- commitment_count: number of commitments
|
|
- summary_hash: short hash of the snapshot for quick correlation
|
|
|
|
These values are intentionally coarse (conservative sums) so a lightweight
|
|
validator can quickly spot obvious infeasibility (e.g., fuel required > node fuel).
|
|
"""
|
|
total_fuel = 0.0
|
|
total_power = 0.0
|
|
max_payload = 0
|
|
count = 0
|
|
|
|
for c in delta.commitments.value():
|
|
if not isinstance(c, dict):
|
|
continue
|
|
count += 1
|
|
try:
|
|
total_fuel += float(c.get("fuel", 0.0))
|
|
except Exception:
|
|
total_fuel += 0.0
|
|
try:
|
|
total_power += float(c.get("power", 0.0))
|
|
except Exception:
|
|
total_power += 0.0
|
|
try:
|
|
max_payload += int(c.get("payload_bytes", 0))
|
|
except Exception:
|
|
max_payload += 0
|
|
|
|
snapshot = delta.snapshot()
|
|
summary_hash = _short_hash(snapshot)
|
|
|
|
return {
|
|
"total_fuel": total_fuel,
|
|
"total_power": total_power,
|
|
"max_payload_bytes": max_payload,
|
|
"commitment_count": count,
|
|
"summary_hash": summary_hash,
|
|
}
|
|
|
|
|
|
def _short_hash(obj: Any) -> str:
|
|
"""Return a short hex digest for the JSON of obj."""
|
|
import hashlib
|
|
|
|
raw = json.dumps(obj, sort_keys=True, separators=(",", ":"))
|
|
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:16]
|