# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-present-a-revoked-aaif-credential-when-its-attestation-chain-was-minted-before-the-revocation-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-present-a-revoked-aaif-credential-when-its-attestation-chain-was-minted-before-the-revocation-epoch.warrant.json
# Markdown SHA-256:   655b76a26261a66a79b4ec58b39c14dd28528743e46d5b1a5befb49e4fe3aefa
# Code AST SHA-256:   61e0608393603612b4a8a6a0d5ba41ac27ecd0001ccf8709b5b53029dcf950aa
# Verified Classes:   TransitiveRevocationError, Hop, Chain
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class TransitiveRevocationError(RuntimeError):
    """Raised when a chain hop is revoked, or revocation state cannot be established."""
    pass


@dataclass
class Hop:
    identity: str
    not_after: float          # unix seconds
    parent: Optional[str]


@dataclass
class Chain:
    hops: list[Hop]           # ordered root -> leaf; root is CA-issued
    revocation_epoch: Optional[float]  # watermark of revocation feed at mint, if present


def verify_chain(chain: Chain, now: float, revoked: set[str],
                 feed_watermark: float, max_cache_staleness: float) -> None:
    # Freshness precondition: the revocation set must be no staler than the
    # verifier's tolerance. If we cannot bound staleness, we cannot check
    # transitive revocation, and we must fail closed on depth > 1.
    cache_age = now - feed_watermark
    if cache_age > max_cache_staleness:
        if len(chain.hops) > 1:
            raise TransitiveRevocationError(
                f"revocation feed stale by {cache_age:.1f}s; "
                f"cannot verify depth-{len(chain.hops)} chain transitively"
            )

    # TTL nesting precondition: child must not outlive parent. A violation
    # means authority was laundered past the parent's expiry.
    for i in range(1, len(chain.hops)):
        if chain.hops[i].not_after > chain.hops[i - 1].not_after + 1e-6:
            raise TransitiveRevocationError(
                f"hop {chain.hops[i].identity} outlives parent "
                f"{chain.hops[i - 1].identity}; TTL nesting violated"
            )

    # Transitive revocation check: every hop, not just the leaf.
    for hop in chain.hops:
        if hop.not_after < now:
            raise TransitiveRevocationError(f"hop {hop.identity} expired at {hop.not_after}")
        if hop.identity in revoked:
            raise TransitiveRevocationError(
                f"hop {hop.identity} is revoked; chain presented at {now} "
                f"carries authority from a withdrawn identity"
            )

    # Depth-2+ chains additionally require a revocation epoch at or after
    # the latest hop's mint, so a future verifier can re-check without
    # trusting this verifier's cache.
    if len(chain.hops) > 1 and chain.revocation_epoch is None:
        raise TransitiveRevocationError(
            "depth>1 chain missing revocation_epoch; not re-verifiable"
        )
