Skip to content

test(gossip,node): harden the rule-2 staked-node gate — reject-path test + Some regression guard (#1222) - #1232

Merged
thiras merged 3 commits into
mainfrom
test/1222-harden-announce-gate
Jul 17, 2026
Merged

thiras merged 3 commits into
mainfrom
test/1222-harden-announce-gate

Conversation

@yigitdot

@yigitdot yigitdot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Implements items 2 and 3 of #1222 — non-blocking hardening follow-ups spun out of the #1170 / #1221 review of the gossip NodeAnnounce rule-2 staked-node gate. Neither is a correctness fix; they close a test-coverage gap and a silent-regression hole.

Item 2 — Subscriber-level not_staked reject integration test

The reject path was proven only at the pure-function level (staked_node_gate_enforced, empty_staked_set_rejects_all). The two-node e2e test wired both sides with AllStaked and exercised only the accept direction — the Err(reject) → "drop + inc_rejected, no insert" arm at the subscriber was untested.

New test non_staked_announce_is_dropped_at_subscriber (crates/node/tests/self_hosted_discovery.rs):

  • Wires the receiver B's gate to a new NoneStaked stub that excludes publisher A.
  • Drives a real NodeAnnounce from A through B's production spawn → subscriber_task → validate_envelope path.
  • Asserts B's peer table stays empty (A never inserted) and the not_staked reject counter increments — proving the announce reached B's validator and was dropped there, not silently lost in the mesh (a metric is needed because NoopMetrics can't observe counters; added a small NotStakedRejectCounter).
  • Reuses the existing ~130-line two-node MemoryLookup mesh harness; bounded 50×100ms wait matches the accept test to avoid CI flake.
  • A final post-loop assertion re-checks non-insertion at teardown (end-state invariant and a guard against future edits to the loop); a comment records that not_staked > 0 attributes the reject to A only under two nodes plus plumtree never echoing to origin.

Item 3 — Assert the runtime wires the gate as Some

The runtime built the gate as Some(Arc::new(NodeStakedNodeSet::new(...))) inline; run() is only reachable via the heavyweight anvil e2e, so a future refactor setting it to None would silently disable rule 2 (fail-open) with every existing test still green — reopening the exact hole #1170 closed.

  • Factored the construction into a named announce_staked_gate() helper in crates/node/src/reputation_wiring.rs that always returns Some, and wired runtime/mod.rs to it.
  • Added a unit test announce_staked_gate_enforces_never_fails_open asserting the returned gate is Some and actually enforces (staked member admitted, non-member rejected), so a refactor returning Some(<admit-all stub>) cannot pass while disabling rule 2. Reuses the existing staker_set_with builder; no anvil e2e needed.

Deferred — Item 1 (enum AnnounceGate vs Option)

Left out by decision: it diverges from the existing ReputationWiring.staked: Option<...> precedent, and #1221's doc-comment tightening ("None is tests-only — no production path disables the gate") already mitigates the immediate risk. Item 3's named helper delivers the same self-documenting-intent benefit without a workspace-wide type change.

Verification

  • cargo fmt -- --check ✓
  • cargo clippy --workspace --all-targets -- -D warnings ✓
  • cargo nextest run -p decdn-node -p decdn-gossip --no-fail-fast → 853 passed, 0 failed
  • Confidence flip-check: temporarily setting B's gate to AllStaked makes the new reject test fail (B learns A), confirming the assertions actually exercise the reject path.
  • Review follow-ups (commits f1be115, 507fcf2) re-verified with cargo fmt --check, cargo clippy -p decdn-node --all-targets -- -D warnings, and the two affected tests.

Addresses items 2 and 3 of #1222. Item 1 (the enum AnnounceGate refactor) is intentionally deferred, so this PR does not close the issue — #1222 stays open to track item 1.

🤖 Generated with Claude Code

…-gate Some regression guard (#1222)

Hardening follow-ups spun out of the #1170/#1221 review of the gossip
rule-2 staked-node gate. Neither is a correctness fix.

Item 2: add a subscriber-level reject integration test
(non_staked_announce_is_dropped_at_subscriber) that wires the receiver's
gate to exclude the publisher, drives a real announce through the
spawn -> subscriber_task -> validate_envelope path, and asserts the peer
table stays empty while the not_staked reject metric increments. Closes
the Err(reject) arm, previously proven only at the pure-function level.

Item 3: factor the runtime's announce-gate construction into a named,
unit-tested announce_staked_gate() helper that always returns Some, so a
future refactor cannot silently drop rule 2 to None (fail-open) with
every existing test still passing.

Item 1 (enum AnnounceGate vs Option) is deferred: it diverges from the
ReputationWiring.staked: Option<...> precedent, and #1221's doc-comment
tightening already mitigates the risk.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@yigitdot, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 9 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: 0681c13a-88be-4c0f-83a3-1e34db54cc7f

📥 Commits

Reviewing files that changed from the base of the PR and between bdf1c4f and 507fcf2.

📒 Files selected for processing (3)
  • crates/node/src/reputation_wiring.rs
  • crates/node/src/runtime/mod.rs
  • crates/node/tests/self_hosted_discovery.rs

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.

@github-actions

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Contracts coverage

Line coverage: 88.92% (2488/2798 lines)
▲ +0.00pp vs base (88.92% → 88.92%)
Run: 29524005537

@yigitdot
yigitdot requested a review from Copilot July 16, 2026 17:44

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 introduces a robust mechanism to enforce ADR 001 rule 2, ensuring that node announcements are only accepted from staked nodes. It adds a new announce_staked_gate helper function in reputation_wiring.rs to guarantee that the admission gate is always initialized, and includes a comprehensive integration test in self_hosted_discovery.rs to verify that non-staked announcements are correctly rejected at the subscriber level. The review feedback highlights a potential race condition in the new test and provides a code suggestion to add a final assertion to ensure the rejection state remains stable.

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 on lines +581 to +585
assert!(
rejected,
"B's subscriber must reject A's announce with not_staked — proving the \
announce reached B's validator and was dropped, not silently lost in the mesh"
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This assertion correctly verifies that a rejection occurred. However, there's a small race condition window: a subsequent announce from node A could theoretically be processed and inserted into B's peer table after the loop breaks but before the test's shutdown sequence begins.

To make the test more robust and cover this edge case, I suggest adding a final assertion after this block to re-verify that node B has not learned about node A, ensuring the rejection state is stable until shutdown.

Suggested change
assert!(
rejected,
"B's subscriber must reject A's announce with not_staked — proving the \
announce reached B's validator and was dropped, not silently lost in the mesh"
);
assert!(
rejected,
"B's subscriber must reject A's announce with not_staked — proving the \
announce reached B's validator and was dropped, not silently lost in the mesh"
);
assert!(
!learned_peer(&b_peers, &a_id, "US").await,
"receiver B must never insert the non-staked publisher A (rule-2 reject), even after rejection is confirmed"
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the final assertion in f1be115, but for a different reason than stated. The described race — a later A announce being inserted after the loop breaks — cannot occur here: B's gate is NoneStaked, whose contains() returns false unconditionally, so validate_envelope returns Err(NotStaked) for every announce from A regardless of timing; there is no code path that inserts A into B's peer table. The per-iteration !learned_peer assertion also already runs on the same iteration that observes the reject, so non-insertion is verified at the moment of rejection.

I still added the post-loop assertion, but as an explicit end-state invariant and a guard against future edits to the loop's break condition, not as a race fix — the comment reflects that.

@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.97% (was 80.96%, ▲ +0.01%) — 48245/59586 lines

File Coverage Lines Δ
crates/node/src/reputation_wiring.rs 95.45% 357/374 ▲ +0.15%
crates/node/src/runtime/mod.rs 49.75% 1083/2177 ▲ +0.05%
History
Commit Total Δ vs base
a057552 80.59% ▲ +0.01%
f1be115 80.96% ▲ +0.01%
507fcf2 (HEAD) 80.97% ▲ +0.01%

…eview)

Add a final post-loop non-insertion assertion so the reject state is
verified right up to teardown and the invariant survives future edits to
the loop's break condition. Not a race fix — NoneStaked rejects
unconditionally, so A can never be admitted — but it documents the
end-state explicitly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@altanoruc

Copy link
Copy Markdown
Contributor

Review

Verified the diff against main: the new test's harness dependencies all line up (spawn_publisher's 8-arg signature with staked as the last param, all six GossipMetrics methods implemented on the stub, AnnounceReject/label() re-exported from decdn_gossip), and the subscriber's Err(reject) arm in service.rs does exactly what the test asserts — inc_rejected(reject.label()) with no peer-table insert. No correctness issues found. The runtime change is a pure refactor (inline Some(...) → named helper), behavior-identical.

What's good

  • The counting metric is the right call: without it, "B never learned A" would also be satisfied by a mesh that never formed, and the PR's flip-check (swapping B's gate to AllStaked makes the test fail) confirms the assertions bite.
  • The in-loop "B must NEVER insert A" invariant checked on every iteration is stronger than a single end-state check — it would catch a transient insert-then-evict.
  • Item 3's tripwire test is small but does close a real fail-open hole: run() is only exercised by the anvil e2e, so a None regression would otherwise ship green.

Suggestions

  1. Strengthen announce_staked_gate_is_always_some to also pin delegation. Right now it only asserts is_some(), so a refactor that returned Some(Arc::new(SomeStub)) — enforcing nothing — would still pass. Since you already have staker_set_with/pk() in scope, two more lines pin the gate to the real registry-backed behavior:

    let member = pk();
    let gate = announce_staked_gate(staker_set_with(member)).expect("gate must enforce");
    assert!(gate.contains(member.as_bytes()));
    assert!(!gate.contains(pk().as_bytes()));

    node_staked_node_set_delegates_to_staker_set covers the inner type, but nothing ties the helper's return value to it.

  2. Minor caveat worth a one-line comment on the reject test: the not_staked counter can't attribute the reject to A specifically. Today that's safe — plumtree never routes a message back to its origin (the is_self_announce comment in service.rs relies on the same fact), so with two nodes the only possible not_staked reject at B is A's announce. But note the self-echo drop happens in the Ok arm, after validation — so if plumtree ever did echo, B's NoneStaked gate would reject B's own echoed announce and satisfy the test without A's announce ever arriving. Not actionable beyond a comment; the metrics API doesn't carry the announcer id.

Nothing blocking — LGTM with suggestion 1 as the one I'd actually make before merge.

…tion (#1232 review)

Item 3 test: assert the gate returned by announce_staked_gate actually
enforces (member admitted, non-member rejected) via .expect(), not just
is_some() — so a refactor returning Some(<admit-all stub>) can't pass
while silently disabling rule 2. Renamed to
announce_staked_gate_enforces_never_fails_open.

Reject test: document that not_staked > 0 attributes to A only because
there are two nodes and plumtree never echoes to origin; otherwise
NoneStaked would reject B's self-echo in validate_envelope (before the
self-echo Ok arm) and satisfy the check without A's announce arriving.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@yigitdot

Copy link
Copy Markdown
Contributor Author

Both suggestions applied in 507fcf2.

1 — gate enforcement pinned. `announce_staked_gate_is_always_some` was renamed to `announce_staked_gate_enforces_never_fails_open` and now asserts the returned gate actually delegates to the staker set (member admitted, non-member rejected) via `.expect(...)`, not just `is_some()` — so a refactor returning `Some()` no longer passes.

2 — attribution caveat documented. Confirmed the ordering: `validate_envelope` runs the staked check first, and the self-echo drop is in the later `Ok(is_self_announce)` arm — so with `NoneStaked`, B's own echoed announce would hit the `Err(NotStaked)` arm and bump the counter before that drop. Added a comment at the counter check noting `not_staked > 0` attributes to A only under (a) two nodes and (b) plumtree not echoing to origin, and to revisit if either changes. No code change — the metrics API carries no announcer id.

@thiras
thiras merged commit 5107fe2 into main Jul 17, 2026
23 checks passed
@thiras
thiras deleted the test/1222-harden-announce-gate branch July 17, 2026 17:34
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.

4 participants