# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-communication-invariants-prevent-message-amplification-storms-in-gossip-protocols.md # Remnant Warrant: https://corpusfetch.com/c/what-communication-invariants-prevent-message-amplification-storms-in-gossip-protocols.warrant.json # Markdown SHA-256: 0ab60fa39a08fd947954f65f2cfed2b5f9efe0b4c44c8d300b299c9a9235661a # Code AST SHA-256: 67fad175befbcb6e2a759f039eec5bca3cd18bd2f4c5c0048e522fb91efc4d95 # Verified Classes: GossipVerdict, GossipMessage, PeerState, GossipManifest, GossipAmplificationGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json class GossipVerdict(Enum): GOSSIP_ALLOWED = auto() TTL_EXPIRED = auto() DUPLICATE_SUPPRESSED = auto() BUDGET_EXHAUSTED = auto() @dataclass(frozen=True) class GossipMessage: message_id: str current_ttl_hops: int priority: str @dataclass(frozen=True) class PeerState: seen_message_ids: frozenset[str] fanout_budget_remaining: int max_ttl_hops: int @dataclass(frozen=True) class GossipManifest: verdict: GossipVerdict directive: str manifest_digest: str class GossipAmplificationGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_message( cls, msg: GossipMessage, state: PeerState ) -> GossipManifest: # Gate 1: TTL if msg.current_ttl_hops >= state.max_ttl_hops: return GossipManifest( verdict=GossipVerdict.TTL_EXPIRED, directive=f"DROP: Message {msg.message_id} exceeded {state.max_ttl_hops} hops.", manifest_digest=cls._hash(f"TTL:{msg.message_id}") ) # Gate 2: Deduplication if msg.message_id in state.seen_message_ids: return GossipManifest( verdict=GossipVerdict.DUPLICATE_SUPPRESSED, directive=f"DROP: Message {msg.message_id} already seen.", manifest_digest=cls._hash(f"DUP:{msg.message_id}") ) # Gate 3: Budget if state.fanout_budget_remaining <= 0: # Critical priority bypasses budget if msg.priority == "CRITICAL": digest = cls._hash(json.dumps({ "message": msg.message_id, "priority": "CRITICAL", "bypass": "BUDGET" }, sort_keys=True)) return GossipManifest( verdict=GossipVerdict.GOSSIP_ALLOWED, directive="PROCEED: Critical priority bypasses fanout budget.", manifest_digest=digest ) return GossipManifest( verdict=GossipVerdict.BUDGET_EXHAUSTED, directive=f"DROP: Fanout budget exhausted for message {msg.message_id}.", manifest_digest=cls._hash(f"BUDGET:{msg.message_id}") ) # Gate 4: Allowed digest = cls._hash(json.dumps({ "message": msg.message_id, "ttl": str(msg.current_ttl_hops), "budget": str(state.fanout_budget_remaining), "priority": msg.priority }, sort_keys=True)) return GossipManifest( verdict=GossipVerdict.GOSSIP_ALLOWED, directive=f"PROCEED: Gossip allowed with {state.fanout_budget_remaining} budget remaining.", manifest_digest=digest )