Skip to content

fix(dash-spv): stop swapping peers on idle rates - #1333

Merged
ZocoLini merged 2 commits into
devfrom
fix/idle-slow-peer-swap
Nov 7, 2026
Merged

ZocoLini merged 2 commits into
devfrom
fix/idle-slow-peer-swap

Conversation

@xdustinface

@xdustinface xdustinface commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1330.

Problem

The flow controller fed every tick with bytes in flight into a peer's serving_rate. At the chain tip the only traffic is a few KB of replies (a mempool getdata, one CFHeaders, one MnListDiff), and their speed is set by the round trip, not by bandwidth. A far peer then drifted to a fraction of a near one, and the supervisor swapped it out as slow long after sync. In a testnet run a healthy peer was swapped out 14 minutes after sync completed:

10:56:16.515  68.67.122.25:19999 serving at 0.01 MB/s vs best 0.05 MB/s — a replacement is worth probing for
10:56:17.895  peer supervisor: swapped out slow 68.67.122.25:19999 for 85.209.243.6:19999

That peer had read only 0.8 MB in total, so it was measured from idle traffic alone.

Fix

  • refactor: the serving-rate sampling moves out of the flow loop into PeerFlow::new and PeerFlow::tick, so it can be tested. Behaviour is unchanged.
  • Sample only while bandwidth bound. A tick counts toward serving_rate only when the peer has bytes in flight and either they reach its cap or the client's link cap (global_cap) binds. These are the two conditions on which the router stops sending. A peer short of both has no more work waiting, so its rate reflects how much it was asked for, not its bandwidth. The link clause matters because the router keeps peers at a similar in-flight to cap ratio. When the global cap binds, fast peers sit well below their own caps, and without it only the slow peer would ever be sampled.

Phases where the pipeline offers less work than the caps allow, such as headers sync and the download endgame, are no longer sampled. Those rates are latency bound, so this is intended. A slow peer is still detected from the bulk filter and block download.

Tests

  • only_bandwidth_bound_service_condemns_a_peer drives PeerFlow::tick for two peers and feeds the result to underserving_peer:
    • latency bound replies at the tip: two peers measured at 1 MB/s, then 50 small replies each, one answered within a tick and one after 8 ticks. No peer may be flagged. With the old gate the far peer is flagged.
    • a slow peer at its own cap: 1 MB/s against 100 KB/s. The slow peer is flagged, before and after the fix.
    • a slow peer held back by the link cap: both peers at half their cap while the link cap binds. The slow peer is flagged. A gate on the peer's own cap alone fails this case.
  • cargo test -p dash-spv --lib: 642 passed (on dev at 4e49ea925).
  • just dash-spv::test: all green, including dashd_sync 38/38 and dashd_masternode 11/11.

PR Hygiene · 5348394

  • Bots — coderabbitai skipped after its own rate limit
  • Self-review — posted; again after any push
  • Build green
  • Approvals — you own every area touched; none needed

When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.

Summary by CodeRabbit

  • Bug Fixes
    • Peer serving-rate measurements now count only periods when the peer or shared connection is at its bandwidth limit. This distinguishes bandwidth-constrained peers from peers whose responses are slow for other reasons, helping ensure peer comparisons reflect bandwidth-bound performance.

@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 26 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 12945f3 and 5348394.

Files selected for processing: 2

  • dash-spv/src/network/manager.rs
  • dash-spv/src/network/peer.rs

No actionable comments were generated in the recent review.

Walkthrough

Serving-rate samples now update only when a peer has in-flight work and is constrained by its own cap or the shared link cap. The flow controller passes shared-link fullness to PeerFlow. Tests cover bandwidth-bound and latency-bound cases.

Changes: Bandwidth-bound peer rate sampling

Layer / File(s) Summary
Serving-rate sampling and validation
dash-spv/src/network/peer.rs, dash-spv/src/network/manager.rs
PeerFlow updates serving-rate samples only when a peer has in-flight work and reaches its own cap or the shared link is full. Tests cover peer-cap and link-cap constraints, and confirm that latency-bound replies do not identify a slow peer.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue 1243 requests tests for peer swaps. This PR adds tests for serving-rate measurement and slow-peer selection.

Suggested reviewers: zocolini

Merge Risk: Low · previous review 12945

A peer may still be replaced unnecessarily when its small replies coincide with another peer filling the link. The remaining risk is bounded and does not prevent merging.

Pre-merge checks: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive [#1330] PeerFlow::tick samples only when the peer or link cap binds. The new test covers idle replies and slow peers at both caps. The PR diff does not move the replacement debug line. The issue pla… Provide the reviewed-head code for underserving_peer and the supervisor wake path to verify the debug line location.
Passed Passed checks: 4 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title describes the main fix: preventing peer swaps caused by idle serving rates.
Out of Scope Changes check Passed [#1330] The flow-controller refactoring, bandwidth-bound sampling, tests, and documentation support the linked issue. The summary shows no unrelated changes.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 2 files.

Full details: Linked Issues check

Explanation

[#1330] PeerFlow::tick samples only when the peer or link cap binds. The new test covers idle replies and slow peers at both caps. The PR diff does not move the replacement debug line. The issue places that line in underserving_peer at an earlier revision, but the head file read was truncated before that function. The available evidence does not establish whether the line was already moved at the reviewed head.

Resolution

Provide the reviewed-head code for underserving_peer and the supervisor wake path to verify the debug line location.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f2dd27c and 12945f3.

Files selected for processing: 2

  • dash-spv/src/network/manager.rs
  • dash-spv/src/network/peer.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@xdustinface
xdustinface force-pushed the fix/idle-slow-peer-swap branch from d891751 to 378f438 Compare November 5, 2026 12:25
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.29%. Comparing base (4e49ea9) to head (5348394).
⚠️ Report is 3 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1333      +/-   ##
==========================================
+ Coverage   85.17%   85.29%   +0.12%     
==========================================
  Files         268      268              
  Lines       68101    68678     +577     
==========================================
+ Hits        58005    58580     +575     
- Misses      10096    10098       +2     
Flag Coverage Δ
core 82.43% <ø> (ø)
rpc 52.10% <ø> (ø)
spv 94.13% <100.00%> (+0.10%) ⬆️
wallet 83.39% <ø> (+0.26%) ⬆️
Files with missing lines Coverage Δ
dash-spv/src/network/manager.rs 89.47% <100.00%> (+2.06%) ⬆️
dash-spv/src/network/peer.rs 90.22% <ø> (+0.22%) ⬆️

... and 11 files with indirect coverage changes

@xdustinface
xdustinface force-pushed the fix/idle-slow-peer-swap branch from 378f438 to d2561cb Compare November 5, 2026 13:51
@xdustinface
xdustinface force-pushed the fix/idle-slow-peer-swap branch from d2561cb to 12945f3 Compare November 6, 2026 05:49
@xdustinface
xdustinface marked this pull request as ready for review November 6, 2026 05:50
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Bots are done — your move: post /self-reviewed.
Full checklist in the description.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Nov 6, 2026
The flow controller fed every tick with bytes in flight into a peer's `serving_rate`. At the chain tip the only traffic is a few KB of replies whose speed is set by the round trip, so a far peer drifted to a fraction of a near one and the supervisor swapped it out as slow long after sync.

`PeerFlow::tick` now samples only while the router can give the peer no more work: its bytes in flight are at its cap, or the client's link cap binds. These are the two conditions the router stops sending on, so a peer short of both is limited by how much it was asked for, not by its bandwidth. Sync-time detection keeps working because bulk download holds peers at one of the two caps.
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added waiting-bots Waiting for the review bots to report on this head and removed waiting-self-review Waiting for the author to post /self-reviewed labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Bots are done — your move: post /self-reviewed.
Full checklist in the description.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. and removed waiting-bots Waiting for the review bots to report on this head labels Nov 6, 2026
@xdustinface

Copy link
Copy Markdown
Collaborator Author

/self-reviewed

@xdustinface
xdustinface requested a review from ZocoLini November 6, 2026 09:18
@github-actions github-actions Bot removed the waiting-self-review Waiting for the author to post /self-reviewed label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Policy satisfied — this can merge.
Full checklist in the description.

@ZocoLini
ZocoLini merged commit d3d1e4f into dev Nov 7, 2026
59 of 60 checks passed
@ZocoLini
ZocoLini deleted the fix/idle-slow-peer-swap branch November 7, 2026 14:33
ZocoLini added a commit that referenced this pull request Nov 7, 2026
#1334 made `underserving_peer` return a `SlowPeer` instead of a `SocketAddr`, and the test #1333 added still compared the result to a peer address, so dev fails to build its tests.
ZocoLini added a commit that referenced this pull request Nov 7, 2026
#1334 made `underserving_peer` return a `SlowPeer` instead of a `SocketAddr`, and the test #1333 added still compared the result to a peer address, so dev fails to build its tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-review-skipped A required review bot did not report; it was skipped by the window or by a person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dash-spv: the supervisor swaps out a "slow" peer while idle at the tip

2 participants