# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-drift-in-a-byzantine-agent-quorum-when-signed-votes-diverge-across-transport-partitions.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-drift-in-a-byzantine-agent-quorum-when-signed-votes-diverge-across-transport-partitions.warrant.json
# Markdown SHA-256:   0da813cac9889b55e3008457523eb12d157b3ce0d41aff100c3161f55b0feca2
# Code AST SHA-256:   a4ff51864842b039a57a43b4508379af72182f7f862aaec8a8002099622e211a
# Verified Classes:   EquivocationDriftError, Vote
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class EquivocationDriftError(RuntimeError):
    """Raised when a vote is counted toward quorum without a valid
    round-scoped non-equivocation attestation, allowing equivocation drift."""
    pass


class Vote:
    def __init__(self, signer, round_num, proposal_hash, attestation=None):
        self.signer = signer
        self.round_num = round_num
        self.proposal_hash = proposal_hash
        self.attestation = attestation  # signed (signer, round) uniqueness claim


def validate_vote_for_quorum(vote, round_barrier_enabled, known_signatures):
    # Local equivocation check: same signer, same round, different hash.
    prior = known_signatures.get((vote.signer, vote.round_num))
    if prior is not None and prior != vote.proposal_hash:
        raise EquivocationDriftError(
            f"local equivocation: signer={vote.signer} round={vote.round_num} "
            f"hashes={prior} vs {vote.proposal_hash}"
        )

    # Drift guard: without a round barrier, require non-equivocation attestation.
    if not round_barrier_enabled:
        if vote.attestation is None:
            raise EquivocationDriftError(
                f"vote from signer={vote.signer} round={vote.round_num} "
                f"lacks non-equivocation attestation; counting it risks drift"
            )
        if not verify_attestation(vote.attestation, vote.signer, vote.round_num):
            raise EquivocationDriftError(
                f"invalid non-equivocation attestation from signer={vote.signer} "
                f"round={vote.round_num}"
            )

    known_signatures[(vote.signer, vote.round_num)] = vote.proposal_hash
    return True


def verify_attestation(attestation, signer, round_num):
    # Placeholder: must bind signer, round, and uniqueness claim under the
    # signer's quorum key. Returns False on any mismatch.
    return (
        attestation.get("signer") == signer
        and attestation.get("round") == round_num
        and attestation.get("unique") is True
        and attestation.get("sig") is not None
    )


def build_commit_certificate(votes, round_barrier_enabled):
    known = {}
    counted = 0
    for v in votes:
        validate_vote_for_quorum(v, round_barrier_enabled, known)
        counted += 1
    if counted < 5:  # 2f+1 for f=2
        raise EquivocationDriftError(
            f"insufficient attested votes: {counted} < 5"
        )
    return {"votes": votes, "count": counted}
