Source code for fleetmesh_core.accounting.market

"""The compute market: what an ask commits to, and what a dispute may admit.

The ask prices the work without describing it. The commitment is what stops the
buyer swapping the job after quotes come back, and the verify mode declared in
the ask is what fixes, in advance, which evidence a later dispute will accept.
"""
from __future__ import annotations

import math
from typing import Any, Mapping, Optional

from ..crypto import crypto

from ..wire import schema_files
from ..wire.interfaces import EventLike, SchemaConstantsDocument, VerifyModeSpec

CONSTANTS: SchemaConstantsDocument = schema_files.load_constants()
MODES: dict[str, VerifyModeSpec] = CONSTANTS['market']['verify_modes']


[docs]def ask_commitment(job_payload: Any) -> str: """SHA-256 over the canonical job spec, which the public ask carries.""" return crypto.sha256(crypto.canonical(job_payload))
[docs]def commitment_holds(ask_event: EventLike, job_payload: Any) -> bool: """The seller checks that the job it was handed is the job that was quoted.""" commit = [t for t in ask_event.get('tags', []) if t and t[0] == 'commit'] return bool(commit) and len(commit[0]) > 1 and commit[0][1] == ask_commitment(job_payload)
[docs]def declared_mode(ask_event: EventLike) -> Optional[str]: v = [t for t in ask_event.get('tags', []) if t and t[0] == 'verify'] if not v or len(v[0]) < 2: return None return v[0][1]
[docs]def leaks_job(ask_event: EventLike) -> list[str]: """An ask naming its module or input is not a confidential job. Enforced in the JSON Schema too; restated here because a relay operator may want to reject at ingestion without running a validator. """ return sorted({t[0] for t in ask_event.get('tags', []) if t and t[0] in ('module', 'input')})
[docs]def admissible_evidence(mode: str, evidence: Mapping[str, Any]) -> tuple[bool, str]: """Does this evidence support a finding under the mode the ask declared? A refutation citing evidence the declared mode does not admit is not a finding. This is what stops a buyer choosing the cheapest verification and then litigating as though it had bought the strongest. """ if mode not in MODES: return False, 'unknown verify mode %r' % mode kind = evidence.get('kind') rerun = {'rerun-divergence'} proof = {'proof-invalid', 'commitment-mismatch'} delivery = {'sealed-key-does-not-open', 'result-hash-mismatch-after-decrypt', 'empty-payload'} schema_bounds = {'schema-validation-failure', 'grammar-constraint-violation'} tee = {'attestation-invalid', 'measurement-mismatch', 'enclave-signer-untrusted'} quasi = {'sampling-divergence-supermajority'} if kind in delivery: return True, 'delivery evidence is admissible under every mode' match mode: case 'sample' | 'dual': if kind in rerun: return True, 'a re-run contradicting the signed result hash' return False, '%s admits re-run evidence, not %r' % (mode, kind) case 'proof': if kind in proof: return True, 'the proof failed against the commitments' return False, 'proof mode admits proof failure, not %r' % kind case 'schema-bounds': if kind in schema_bounds: return True, 'output failed validation against the committed schema bounds' return False, 'schema-bounds admits schema validation failure, not %r' % kind case 'tee-attestation': if kind in tee: return True, 'the hardware enclave attestation or measurement failed' return False, 'tee-attestation admits hardware attestation failure, not %r' % kind case 'quasi-deterministic': if kind in quasi: return True, 'supermajority peer sampling contradicted executor output' return False, 'quasi-deterministic admits sampling divergence, not %r' % kind case _: return False, '%s mode admits delivery evidence only, not %r' % (mode, kind)
[docs]def needs_plaintext(mode: str) -> bool: """Whether adjudicating under this mode requires holding the job. The hinge of the confidentiality argument: only the modes that answer False let a third party adjudicate a job it was never given. """ return bool(MODES[mode]['needs_plaintext'])
[docs]def jobs_to_detect(sample_rate: float, cheat_fraction: float, confidence: float = 0.9) -> float: """ln(1-c) / ln(1-pf) - how many jobs before a cheat is caught. Stated in the specification and worth having executable, because the honest version of "we will catch them" is a number that depends on a job rate nobody has measured. """ pf = sample_rate * cheat_fraction if pf <= 0: return float('inf') return math.log(1 - confidence) / math.log(1 - pf)