# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-byzantine-quorum-certificates-as-invalid-when-the-signing-set-is-valid-but-the-epoch-context-has-silently-adv.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-byzantine-quorum-certificates-as-invalid-when-the-signing-set-is-valid-but-the-epoch-context-has-silently-adv.warrant.json
# Markdown SHA-256:   796f62ef5b63e816fe440477aa6c165410e6ebfecb9edf6a3957c6bd13160695
# Code AST SHA-256:   80aba9cc9b9bec03ff908cfbac9c1cad3303e50f80529ab9346c3209163be9c2
# Verified Classes:   StaleEpochQuorumError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StaleEpochQuorumError(RuntimeError):
    """Raised when a quorum certificate is numerically sufficient but not
    a quorum of the target epoch's configuration."""
    pass


def validate_commit_certificate(
    cert_signer_set: set[str],
    cert_epoch: int,
    target_epoch: int,
    members_by_epoch: dict[int, set[str]],
    fault_bound_by_epoch: dict[int, int],
) -> None:
    if cert_epoch is None:
        raise StaleEpochQuorumError(
            "certificate carries no epoch binding; cannot validate against any configuration"
        )

    if cert_epoch not in members_by_epoch or target_epoch not in members_by_epoch:
        raise StaleEpochQuorumError(
            f"unknown configuration: cert_epoch={cert_epoch} target_epoch={target_epoch}"
        )

    target_members = members_by_epoch[target_epoch]
    target_threshold = 2 * fault_bound_by_epoch[target_epoch] + 1

    # The check that the coordinator skipped: intersection with the TARGET set,
    # not the cardinality of the signer set in its originating epoch.
    effective_votes = cert_signer_set & target_members

    if len(effective_votes) < target_threshold:
        raise StaleEpochQuorumError(
            f"certificate from epoch {cert_epoch} has {len(cert_signer_set)} signatures "
            f"but only {len(effective_votes)} fall within epoch {target_epoch} membership "
            f"(threshold {target_threshold}); signers outside target set: "
            f"{sorted(cert_signer_set - target_members)}"
        )
