Skip to content

refactor(gossip): name the announce gate's disabled state (#1222) - #1335

Merged
thiras merged 4 commits into
mainfrom
feat/1222-announce-gate-enum
Jul 21, 2026
Merged

thiras merged 4 commits into
mainfrom
feat/1222-announce-gate-enum

Conversation

@yigitdot

@yigitdot yigitdot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Item 1 of #1222 — name the gossip rule-2 staked-node gate's disabled state at the type level. Items 2 and 3 shipped in #1232; this closes the remaining item.

Problem

The announce gate was threaded as Option<&dyn StakedNodeSet> (validate_envelope) and Option<Arc<dyn StakedNodeSet>> (spawn / subscriber_task), where None = fail-open (accept any signature-valid announce) and Some = enforce ADR 001 rule 2. These two safety-opposite states were distinguished only by nullability. Worse, ReputationWiring.staked is the identically-typed Option<Arc<dyn StakedNodeSet>> with opposite polarity (None = fail-closed, reputation gossip not wired) — two same-typed fields, opposite safety meaning.

Change

Replace the announce-path Option with a named enum:

pub enum AnnounceGate<S> { Enforce(S), Disabled }
pub type OwnedAnnounceGate = AnnounceGate<Arc<dyn StakedNodeSet>>;
  • validate_envelope takes AnnounceGate<&dyn StakedNodeSet>; GossipService::spawn / subscriber_task take the owned OwnedAnnounceGate alias, bridged via OwnedAnnounceGate::as_gate() (replacing the old Option::as_deref).
  • announce_staked_gate now returns AnnounceGate::Enforce, never the fail-open Disabled; its announce_staked_gate_enforces_never_fails_open unit test destructures the variant.
  • ReputationWiring.staked is left as Option — a wiring-presence flag with different (fail-closed) semantics, exactly the precedent the issue flagged.
  • Manual variant-only Debug (since StakedNodeSet isn't Debug-bound), derive(Clone, Copy) (the borrowed instantiation is Copy).

Pure refactor: no behavior change — rule-2 enforcement is byte-for-byte identical.

Verification

Closes #1222.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Security
    • Gossip node announcements are now validated using an explicit admission gate, consistently enforcing staking eligibility rather than relying on “optional” configuration.
    • Removes the possibility of fail-open behavior during rule-2 membership checks.
  • Reliability
    • Gossip service wiring and self-hosted discovery tests were updated to use the new gate contract, keeping acceptance/rejection behavior consistent in both production and testing.
    • Debug output for the gate now avoids exposing sensitive membership details.

Replace the announce-path `Option<_ StakedNodeSet>` gate with a named
`AnnounceGate { Enforce, Disabled }` enum so the fail-open intent is
self-documenting at every call site. The old `Option` distinguished two
safety-opposite states (`None` = accept any signature-valid announce,
`Some` = enforce ADR 001 rule 2) only by nullability — and was
identically typed to `ReputationWiring.staked`, whose `None` has the
opposite (fail-closed) polarity.

- `validate_envelope` takes `AnnounceGate<&dyn StakedNodeSet>`;
  `GossipService::spawn` / `subscriber_task` take the owned
  `OwnedAnnounceGate` alias, bridged via `OwnedAnnounceGate::as_gate`.
- `announce_staked_gate` now returns `AnnounceGate::Enforce`, never the
  fail-open `Disabled`; its unit test destructures the variant.
- `ReputationWiring.staked` is left as `Option` (a wiring-presence flag,
  different semantics).

Pure refactor: no behavior change; rule-2 enforcement is identical.

Closes #1222.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: c08a54b1-9e05-403a-8f0c-01cfc78e8f25

📥 Commits

Reviewing files that changed from the base of the PR and between ebb27fb and c01f2eb.

📒 Files selected for processing (2)
  • crates/gossip/src/service.rs
  • crates/gossip/src/validation.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/gossip/src/service.rs
  • crates/gossip/src/validation.rs

Walkthrough

Changes

The PR replaces optional staked-node admission with typed AnnounceGate states. Gossip validation and service wiring now accept enforcing or disabled gates, while production node wiring always creates an enforcing gate and tests pass explicit gate states.

Announce gate migration

Layer / File(s) Summary
Typed gate contract and validation
crates/gossip/src/validation.rs, crates/gossip/src/lib.rs
Adds AnnounceGate, OwnedAnnounceGate, and as_gate(), updates validate_envelope, and re-exports the new types.
GossipService integration
crates/gossip/src/service.rs
Passes OwnedAnnounceGate through GossipService and subscriber_task, using it during envelope validation; updates service tests for Disabled.
Node runtime and discovery wiring
crates/node/src/reputation_wiring.rs, crates/node/src/runtime/mod.rs, crates/node/tests/self_hosted_discovery.rs
Creates always-enforcing gates from the staker set, passes them into gossip, and updates discovery tests with explicit enforcing gates.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant NodeRuntime
  participant GossipService
  participant subscriber_task
  participant validate_envelope
  NodeRuntime->>GossipService: pass announce_gate
  GossipService->>subscriber_task: pass OwnedAnnounceGate
  subscriber_task->>validate_envelope: validate envelope with gate.as_gate()
  validate_envelope-->>subscriber_task: accept or reject NodeAnnounce
Loading

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces the use of Option<Arc<dyn StakedNodeSet>> with a new AnnounceGate enum to explicitly distinguish between enforcing staked membership and failing open. This type safety improvement is propagated across the gossip service, validation logic, runtime, and tests. The feedback suggests importing AnnounceGate in service.rs to prevent cargo doc link resolution failures in the updated documentation.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread crates/gossip/src/service.rs Resolved
The `GossipService::spawn` doc links to `AnnounceGate::{Enforce,Disabled}`,
but `AnnounceGate` is not imported in service.rs (only `OwnedAnnounceGate`
is used in code) — breaking `cargo doc -D warnings` in CI. Importing the
type instead trips `unused_imports` under clippy `-D warnings`, since it is
referenced only in the doc comment. Point the links at the full crate path
so both jobs pass without an otherwise-unused import.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 81.74% (was 81.72%, ▲ +0.02%) — 51417/62904 lines

File Coverage Lines Δ
crates/gossip/src/service.rs 80.46% 667/829 ● 0.00%
crates/gossip/src/validation.rs 100.00% 367/367 ● 0.00%
crates/node/src/reputation_wiring.rs 95.19% 356/374 ▼ 0.27%
crates/node/src/runtime/mod.rs 46.26% 1106/2391 ▲ +0.02%
History
Commit Total Δ vs base
d042d31 81.71% ▼ 0.01%
bc83b16 81.72% ● 0.00%
c01f2eb (HEAD) 81.74% ▲ +0.02%

…contained

Post-review polish (PR #1335, no behavior change):
- Note that `Copy` on `AnnounceGate` is conditional — it applies only to the
  borrowed `&dyn` form; the owned `Arc` alias is `Clone`-only — so the derive
  isn't misread as copying the set.
- Reword the `AnnounceGate` and `as_gate` docs to state what they are/do rather
  than what they replaced (the removed `Option` / `Option::as_deref`), so the
  docs don't become dangling references.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Contracts coverage

Line coverage: 88.94% (2500/2811 lines)
▲ +0.00pp vs base (88.94% → 88.94%)
Run: 29792569294

@yigitdot
yigitdot requested a review from thiras July 20, 2026 21:22
Two low-severity coverage gaps from the #1335 review:

- `as_gate_preserves_variant`: the owned→borrowed projection
  (`OwnedAnnounceGate::as_gate`) was only exercised for the `Enforce`
  arm via the multi-node integration tests; the `Disabled` arm never ran
  through it. Pin both arms (Enforce hit/miss + Disabled) directly.
- `debug_reports_variant_only`: the hand-written variant-only `Debug`
  (written that way because the `S` handle isn't `Debug`) had no test;
  pin the format strings so a future derive-simplify that leaks the set
  contents is caught.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras

thiras commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review summary (5-agent pass)

Ran a comprehensive multi-agent review (general code, type-design, comments, silent-failure, test-coverage). Approve — zero critical, zero important issues. The "no behavior change" claim holds: None → Disabled / Some → Enforce map exactly, as_gate() mirrors the old as_deref(), and the full decdn-gossip + decdn-node suites pass (949/949) with clippy and -D warnings docs clean.

Highlights the reviewers called out:

  • Fail-open is now unreachable in production — the sole producer announce_staked_gate unconditionally returns Enforce; every Disabled site is #[cfg(test)]; no Default backdoor; empty registry fails closed.
  • Conditional Copy is sound, not a footgun — impl<S: Copy> means the Arc-wrapping OwnedAnnounceGate is Clone-only; only the borrowed &dyn form is Copy.
  • Guard test correctly hardened — announce_staked_gate_enforces_never_fails_open now destructures Enforce and asserts both member-admitted / non-member-rejected, defeating both the drop-to-Disabled and admit-everyone-stub regressions.

Follow-up applied in this PR (c01f2eb)

Two low-severity coverage gaps the review surfaced, now closed:

  • as_gate_preserves_variant — the owned→borrowed projection was only exercised for the Enforce arm via the integration tests; now both arms (Enforce hit/miss + Disabled) are pinned directly.
  • debug_reports_variant_only — pins the hand-written variant-only Debug so a future derive-simplify or set-content leak is caught.

Deferred (out of scope)

The one design note left open: ReputationWiring.staked is still Option<Arc<dyn StakedNodeSet>> with the inverse polarity (None = fail-closed) — the exact "same type, opposite meaning" hazard this enum removed for the announce path, now one field over. Deliberate for this PR; tracked as a follow-up.

@thiras
thiras merged commit e3f229a into main Jul 21, 2026
22 checks passed
@thiras
thiras deleted the feat/1222-announce-gate-enum branch July 21, 2026 01:17
thiras pushed a commit that referenced this pull request Jul 21, 2026
… states (#1338) (#1340)

* refactor(gossip): name the reputation-report gate's staked-membership states (#1338)

`ReputationWiring.staked` was an `Option<Arc<dyn StakedNodeSet>>` whose `None`
means fail-CLOSED, sitting one field away from the `AnnounceGate<S>` introduced
by #1335 whose `Disabled` means fail-OPEN. Two identically-shaped gates with
opposite polarity is the exact hazard #1222 set out to remove.

Give the reputation path its own `ReportGate<S> { Enforce(S), Disabled }`
(plus `OwnedReportGate`), mirroring `AnnounceGate`'s shape: hand-written
`Debug` naming the variant, and a hand-written `Default` (the trait-object
handle has none) that keeps the unwired state `Disabled`. Both enums carry a
doc cross-reference to the other's inverse polarity. Node-side, the runtime
now builds the gate via a named `report_staked_gate` constructor alongside
`announce_staked_gate`.

No behavior change: `sink` and the gate remain one combined check with the
same warning, and `ReputationWiring::default()` still runs no reputation
gossip.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* docs+test(gossip): correct ReportGate docs, name both wiring halves in the warning

Post-review fixes on #1340 (no wire or behavior change).

- Derive `Default` on `ReportGate` with `#[default]` on `Disabled` and drop the
  hand-written impl. Its doc justified itself with "a derive would demand
  `S: Default`", which is false: `#[derive(Default)]` on an enum with a
  `#[default]` unit variant emits an unbounded impl. Verified against the exact
  shape on rustc 1.94 / edition 2024 — the enum compiles for
  `ReportGate<Arc<dyn StakedNodeSet>>` while the struct control case does not.
- `ReputationWiring`'s doc claimed "the publisher needs `report_drain`". The
  publisher spawns on the same enforcing branch as the subscriber, so it also
  needs `sink` and `Enforce` — as this PR's own
  `disabled_report_gate_spawns_no_reputation_tasks` asserts.
- The unwired warning said "no reputation sink/staker wired", which reads as
  "neither", but a wired sink with a `Disabled` gate trips it too. Report
  `sink_wired` and `gate_enforcing` as separate fields, via a new
  `ReportGate::is_enforcing`.
- Drop the "a sink with no gate would admit unvetted reports" comment: there is
  no such state, and `reputation_subscriber_task` takes the set by value, so an
  ungated subscriber is unrepresentable.
- Stop defining the gate by the `Option` it replaced (`reputation.rs`,
  `reputation_wiring.rs`) and stop re-describing the fail-closed mechanism in
  `validation.rs` — bc83b16 reworded `AnnounceGate` away from exactly this so
  the docs would not become dangling references. Also soften "every
  signature-valid announce is admitted": `AnnounceGate::Disabled` skips only the
  membership check.
- New `report_gate_debug_reports_variant_only`, mirroring `AnnounceGate`'s test,
  pins both `Debug` arms. Confirmed fails-before: mislabelling the `Enforce` arm
  as `"ReportGate::Disabled"` was caught by this test alone, with the other 69
  gossip tests green.

cargo clippy --workspace --all-targets -D warnings, cargo fmt --check,
RUSTDOCFLAGS=-D warnings cargo doc, cargo deny check, and 2520/2520 workspace
tests all pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow-ups from #1170 review: harden the gossip rule-2 staked-node gate

2 participants