# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-apply-a-cross-org-slashing-verdict-the-quorum-intersection-invariant-under-colluding-reputation-federations.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-apply-a-cross-org-slashing-verdict-the-quorum-intersection-invariant-under-colluding-reputation-federations.warrant.json
# Markdown SHA-256:   c7e5ef1c73c3508bd88e0919c4293beb7864ccb299d2e36d079d224c7d5fafb2
# Code AST SHA-256:   db896c2b934c1d9bf7f6a159f372b8373c39ed20bf0f55799eff5febfc3c8cea
# Verified Classes:   UnenforceableVerdictError, Verdict, TrustRoot
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class UnenforceableVerdictError(RuntimeError):
    """Raised when a cross-org slashing verdict cannot be enforced under the local trust root."""


@dataclass(frozen=True)
class Verdict:
    issuer: str
    signer_set: FrozenSet[str]          # actual signers, not declared validators
    epoch: int
    pinned_genesis: str


@dataclass(frozen=True)
class TrustRoot:
    pinned_genesis: str
    weights: dict                       # validator -> weight under OUR root
    threshold: float                    # t_B, as fraction of total weight
    current_epoch: int


def enforce_verdict(verdict: Verdict, root: TrustRoot) -> None:
    if verdict.pinned_genesis != root.pinned_genesis:
        raise UnenforceableVerdictError(
            f"issuer genesis {verdict.pinned_genesis!r} not anchored in local trust root"
        )
    if verdict.epoch < root.current_epoch:
        raise UnenforceableVerdictError(
            f"verdict epoch {verdict.epoch} predates trust-root rotation {root.current_epoch}"
        )

    total = sum(root.weights.values())
    if total <= 0:
        raise UnenforceableVerdictError("empty local trust root; no weight to evaluate against")

    # Weight of the intersection, measured under OUR root -- never the issuer's.
    intersection_weight = sum(
        w for validator, w in root.weights.items() if validator in verdict.signer_set
    )
    fraction = intersection_weight / total

    if fraction < root.threshold:
        raise UnenforceableVerdictError(
            f"quorum intersection weight {fraction:.3f} < local threshold "
            f"{root.threshold:.3f}; verdict valid but unenforceable"
        )
    # Only here may the slash be applied.
