Skip to content

feat(dash-spv): read the masternode list only while it is current - #1466

Draft
lklimek wants to merge 5 commits into
feat/provider-key-lookup-by-hashfrom
feat/masternode-list-freshness
Draft

lklimek wants to merge 5 commits into
feat/provider-key-lookup-by-hashfrom
feat/masternode-list-freshness

Conversation

@lklimek

@lklimek lklimek commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

TL;DR: dash-spv can now hand out its masternode list only while that list is current, and says why when it is not, so callers stop using a list that is stale, restored from storage, or left over from a stopped client.

User story

As a wallet or application developer using dash-spv, I want to read the masternode list only while it reflects the current chain, to achieve decisions (for example about masternode keys) that are never based on an outdated list.

Scenario

Base flow

An application asks the SPV client for its masternode list, for example right after start-up, after the host wakes from sleep, or while list updates keep failing.

Actual behavior

latest_masternode_list returns whatever list the client holds: one restored from storage before the client has synced, one a stopped client still keeps, one whose tip updates keep failing while masternode sync stays Synced, or one from before the host slept. Callers have to guess freshness from separate progress reads.

Expected behavior

The application gets the newest list only when it is current. Otherwise it gets a typed reason (Disabled, NotRunning, Syncing, TipStale, BehindTip) and can wait or tell the user.

Detailed discussion

What was done

This PR is stacked atop #1465 (feat/provider-key-lookup-by-hash); the two share no code.

  • Adds DashSpvClient::current_masternode_list and current_masternode_list_blocking. They return the newest list, whose known_height is the height it describes, only while:
    • the client runs and masternode sync is enabled;
    • header and masternode sync are Synced;
    • the header tip is not stale: no older than MAINNET_MAX_TIP_AGE (30 min) on Mainnet or TESTNET_MAX_TIP_AGE (2 h) on Testnet. Regtest and devnets mine on demand, so their tip age is not checked;
    • the list is at most MASTERNODE_LIST_TIP_TOLERANCE (2) blocks below the header tip, measured against the header manager's tip when it is ahead of reported progress.
  • Otherwise they return MasternodeListNotCurrentError: Disabled, NotRunning, Syncing, TipStale { tip_time } or BehindTip.
  • The decision is made under the sync coordinator lock, so a list from an earlier run is never reported current.
  • The block-header sync progress records the header tip's block time (tip_time), which the age check uses.

Not included: the sync state machine is unchanged. Making masternode sync leave Synced when list updates stop applying is left to a follow-up.

Testing

  • Unit tests in dash-spv/src/client/queries.rs: a list within two blocks of the tip is current; one further behind is not; the header manager's tip is used when ahead; an old header tip is TipStale; Regtest and devnet lists do not age; a client without masternode sync reports Disabled; a stored list is not current before sync catches up; no list is current while the client is stopped.
  • dash-spv/tests/dashd_masternode/tests_sync.rs pins the header tip time the freshness check relies on.

Prior work

🤖 Generated with Claude Code

https://claude.ai/code/session_01WunYYk4eUkHuGstPtj83j4

🤖 Co-authored by Claudius the Magnificent AI Agent

lklimek and others added 5 commits November 21, 2026 01:06
Add `DashSpvClient::current_masternode_list` and its blocking twin. They return the newest masternode list, whose `known_height` is the height it describes, only while it follows the chain. Otherwise they return a `MasternodeListNotCurrent` saying why: masternode sync is disabled, the client is not running, sync has not caught up since the client started, or the list is behind the header tip.

`latest_masternode_list` returns any stored list: one restored from storage before the client synced, one a stopped client still holds, or one whose tip updates keep failing while masternode sync stays `Synced`. Callers had to guess freshness from separate progress reads.

A list is current when the client runs, header and masternode sync are both `Synced`, and the list is at most two blocks below the header tip, which covers one tip diff in flight. The running flag, the progress and the list are read under the sync coordinator lock, so `run` and `stop` cannot swap the progress in between and a list from an earlier run is never reported current.

The sync state machine is unchanged: masternode sync still stays `Synced` when tip updates fail, and `BehindTip` reports that case to readers of the list.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYk4eUkHuGstPtj83j4
`current_masternode_list` judged freshness only against the local header tip. After the host wakes from sleep, or while peers relay nothing, that tip is as old as the list, so an hours-old list was reported current. Return the new `TipStale` when the tip header is older than `MAINNET_MAX_TIP_AGE` (30 min) or `TESTNET_MAX_TIP_AGE` (2 h). Regtest and devnets mine on demand and are exempt. The tip header's block time is carried in `BlockHeadersProgress`, so the check reads the same progress snapshot without touching storage.

Also from review:
- compare the list against the higher of the header manager's tip and the masternode manager's copy of it;
- return `NotRunning` before taking the coordinator lock, which `stop` holds while its tasks drain;
- report `Disabled` when a running client has no masternode sync, such as after `update_config` turned it off;
- export `MASTERNODE_LIST_TIP_TOLERANCE` and use it in the docs and tests;
- rename the error to `MasternodeListNotCurrentError`, like the crate's other error types;
- document that `Syncing` also follows a loss of all peers;
- test the run-again window on the same client and check the restarted client's list against the engine.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYk4eUkHuGstPtj83j4
Assert that `BlockHeadersManager` records the tip header's block time when it starts from stored headers and when it stores a batch. Without it, `current_masternode_list` would report `TipStale` on mainnet 30 minutes after start.

Document that the tip age is measured against the host clock, keep the raw Unix timestamp out of the `TipStale` message, and document its `tip_time` field.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYk4eUkHuGstPtj83j4
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.45946% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.59%. Comparing base (31fbba5) to head (2707315).

Files with missing lines Patch % Lines
dash-spv/src/client/queries.rs 99.41% 1 Missing ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           feat/provider-key-lookup-by-hash    #1466      +/-   ##
====================================================================
+ Coverage                             86.55%   86.59%   +0.03%     
====================================================================
  Files                                   271      271              
  Lines                                 71045    71218     +173     
====================================================================
+ Hits                                  61491    61668     +177     
+ Misses                                 9554     9550       -4     
Flag Coverage Δ
core 82.99% <ø> (ø)
rpc 52.10% <ø> (ø)
spv 94.68% <99.45%> (+0.05%) ⬆️
wallet 85.40% <ø> (-0.01%) ⬇️
Files with missing lines Coverage Δ
dash-spv/src/client/mod.rs 100.00% <ø> (ø)
dash-spv/src/error.rs 33.33% <ø> (ø)
dash-spv/src/lib.rs 46.66% <ø> (ø)
dash-spv/src/sync/block_headers/manager.rs 95.29% <100.00%> (+0.64%) ⬆️
dash-spv/src/sync/block_headers/progress.rs 96.10% <100.00%> (+0.38%) ⬆️
dash-spv/src/client/queries.rs 88.73% <99.41%> (+44.82%) ⬆️

... and 7 files with indirect coverage changes

This branch has not been deployed

No deployments
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.

1 participant