Skip to content

feat(ffi): add dash_spv_ffi_config_clear_peers#591

Open
QuantumExplorer wants to merge 3 commits intov0.42-devfrom
fix/remove-hardcoded-regtest-peer
Open

feat(ffi): add dash_spv_ffi_config_clear_peers#591
QuantumExplorer wants to merge 3 commits intov0.42-devfrom
fix/remove-hardcoded-regtest-peer

Conversation

@QuantumExplorer
Copy link
Copy Markdown
Member

@QuantumExplorer QuantumExplorer commented Mar 29, 2026

Summary

  • Adds dash_spv_ffi_config_clear_peers() FFI function so iOS/Swift consumers can remove default peers before adding custom ones

Problem

When creating an SPV config for regtest via dash_spv_ffi_config_new(FFINetwork::Regtest), a default peer at 127.0.0.1:19899 is baked in. This doesn't match dashmate's Docker setup which maps Core P2P to port 20001. The iOS app calls dash_spv_ffi_config_add_peer("127.0.0.1:20001") but the dead 19899 peer remains, causing reconnect spam and intermittent QRInfo timeouts when requests get routed to it.

Rust consumers can already do config.peers.clear() since the field is pub, but FFI consumers had no way to clear defaults.

Usage

// Create config (regtest gets default 127.0.0.1:19899)
FFIClientConfig *config = dash_spv_ffi_config_new(FFINetwork_Regtest);

// Clear defaults, then add the actual peer
dash_spv_ffi_config_clear_peers(config);
dash_spv_ffi_config_add_peer(config, "127.0.0.1:20001");

Test plan

  • Existing dash-spv-ffi config tests pass (9/9)
  • Existing dash-spv unit tests pass (243/243)
  • Verify from Swift/iOS that clear_peers + add_peer results in only the intended peer

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added the ability to clear peer connections from client configuration, allowing programs to reset configured peers programmatically.
  • Documentation
    • Updated API docs to include the new configuration operation and its safety requirements for passing valid configuration handles during the call.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 29, 2026

Warning

Rate limit exceeded

@QuantumExplorer has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 16 minutes and 45 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 16 minutes and 45 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a2d469db-0d8a-436a-af32-c1fc1c510d52

📥 Commits

Reviewing files that changed from the base of the PR and between 93eed45 and a785f22.

📒 Files selected for processing (1)
  • dash-spv-ffi/tests/test_config.rs
📝 Walkthrough

Walkthrough

Added a new exported FFI function dash_spv_ffi_config_clear_peers that clears the peers collection on an FFIClientConfig after performing a null check. Documentation (FFI_API.md) was updated to list the new function and its safety requirements.

Changes

Cohort / File(s) Summary
FFI Config Implementation
dash-spv-ffi/src/config.rs
Added pub unsafe extern "C" fn dash_spv_ffi_config_clear_peers(config: *mut FFIClientConfig) -> i32 which null-checks the pointer and calls cfg.peers.clear(), returning FFIErrorCode::Success.
Documentation
dash-spv-ffi/FFI_API.md
Updated API docs: total FFI functions incremented (40 → 41). Added dash_spv_ffi_config_clear_peers(...) -> i32 under the config section with safety notes and usage summary.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hop in code with whiskers bright,
I clear the peers with gentle might.
A null check first, then tidy sweep—
Configs rest easy, dreams of sleep. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a new FFI function for clearing peers from a Dash SPV configuration.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/remove-hardcoded-regtest-peer

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

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

@github-actions
Copy link
Copy Markdown

⚠️ FFI Documentation Update Required

The FFI API documentation is out of date. Please regenerate it by running:

For key-wallet-ffi:

cd key-wallet-ffi
make update-docs

For dash-spv-ffi:

cd dash-spv-ffi
make update-docs

Then commit the changes:

git add */FFI_API.md
git commit -m "docs: update FFI API documentation"

This ensures the documentation stays in sync with the actual FFI functions.

@github-actions
Copy link
Copy Markdown

⚠️ SPV tests hit a SIGSEGV, but single-test isolation found no consistent culprit. See artifacts for logs.

@QuantumExplorer QuantumExplorer changed the base branch from v0.40-dev to v0.42-dev March 29, 2026 07:06
When creating an SPV config for regtest, a default peer at
127.0.0.1:19899 is added. This doesn't match dashmate's Docker
setup which maps Core P2P to different ports (e.g. 20001). FFI
consumers (iOS/Swift) couldn't clear these defaults before adding
their own peers, causing the SPV client to enter exclusive mode
with a dead peer alongside the intended one.

Adds dash_spv_ffi_config_clear_peers() so FFI consumers can remove
default peers before adding custom ones via add_peer.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@QuantumExplorer QuantumExplorer force-pushed the fix/remove-hardcoded-regtest-peer branch from 4d7ea6e to de085dc Compare March 29, 2026 07:12
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@QuantumExplorer QuantumExplorer force-pushed the fix/remove-hardcoded-regtest-peer branch from de085dc to 93eed45 Compare March 29, 2026 07:15
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
dash-spv-ffi/src/config.rs (1)

169-177: Clarify the lifecycle constraint in docs.

At Line 178, this mutates only the config object. Since dash_spv_ffi_client_new clones config at creation time, calling this after client creation has no effect on the running client. Please make that explicit in this doc block to prevent integration misuse.

Suggested doc update
 /// Removes all configured peers from the configuration
 ///
 /// This is useful when the caller wants to start with a clean slate
 /// before adding custom peers via `dash_spv_ffi_config_add_peer`.
+/// Must be called before `dash_spv_ffi_client_new`; the client clones
+/// configuration at creation time, so later changes are not applied.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@dash-spv-ffi/src/config.rs` around lines 169 - 177, Update the doc comment
for the function that removes all peers (the doc block above
dash_spv_ffi_config_remove_all_peers) to explicitly state this mutates only the
FFIClientConfig instance and does not affect any already-created client because
dash_spv_ffi_client_new clones the config at creation time; mention that callers
must remove peers before calling dash_spv_ffi_client_new if they expect the
running client to reflect the change and reiterate the existing safety
requirement that the config pointer must remain valid for the duration of the
call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@dash-spv-ffi/src/config.rs`:
- Around line 178-184: Add a colocated unit test under #[cfg(test)] in the same
file that constructs a ClientConfig with at least one peer, wraps it in an
FFIClientConfig (matching how other tests do it), calls the exported unsafe
function dash_spv_ffi_config_clear_peers within an unsafe block, asserts the
function returns FFIErrorCode::Success as i32, and then asserts the underlying
ClientConfig.peers is empty; reference the types and symbols FFIClientConfig,
ClientConfig, and dash_spv_ffi_config_clear_peers when locating where to add the
test.

---

Nitpick comments:
In `@dash-spv-ffi/src/config.rs`:
- Around line 169-177: Update the doc comment for the function that removes all
peers (the doc block above dash_spv_ffi_config_remove_all_peers) to explicitly
state this mutates only the FFIClientConfig instance and does not affect any
already-created client because dash_spv_ffi_client_new clones the config at
creation time; mention that callers must remove peers before calling
dash_spv_ffi_client_new if they expect the running client to reflect the change
and reiterate the existing safety requirement that the config pointer must
remain valid for the duration of the call.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 71f7a909-12c0-4523-9030-85bb2c0aadd4

📥 Commits

Reviewing files that changed from the base of the PR and between fcdffa7 and de085dc.

📒 Files selected for processing (1)
  • dash-spv-ffi/src/config.rs

Comment on lines +178 to +184
pub unsafe extern "C" fn dash_spv_ffi_config_clear_peers(config: *mut FFIClientConfig) -> i32 {
null_check!(config);

let cfg = unsafe { &mut *((*config).inner as *mut ClientConfig) };
cfg.peers.clear();
FFIErrorCode::Success as i32
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Add direct unit coverage for the new exported function.

Lines 178-184 introduce new FFI behavior but there is no colocated test in this module diff. Please add a unit test that verifies peers are removed (e.g., create config with default peer, call clear, assert peer list is empty).

As per coding guidelines, "Write unit tests for new functionality in Rust code" and "Place unit tests alongside code with #[cfg(test)] attribute".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@dash-spv-ffi/src/config.rs` around lines 178 - 184, Add a colocated unit test
under #[cfg(test)] in the same file that constructs a ClientConfig with at least
one peer, wraps it in an FFIClientConfig (matching how other tests do it), calls
the exported unsafe function dash_spv_ffi_config_clear_peers within an unsafe
block, asserts the function returns FFIErrorCode::Success as i32, and then
asserts the underlying ClientConfig.peers is empty; reference the types and
symbols FFIClientConfig, ClientConfig, and dash_spv_ffi_config_clear_peers when
locating where to add the test.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.05%. Comparing base (fcdffa7) to head (a785f22).

Additional details and impacted files
@@              Coverage Diff              @@
##           v0.42-dev     #591      +/-   ##
=============================================
- Coverage      67.40%   67.05%   -0.36%     
=============================================
  Files            318      318              
  Lines          67020    67026       +6     
=============================================
- Hits           45175    44943     -232     
- Misses         21845    22083     +238     
Flag Coverage Δ
core 75.21% <ø> (ø)
ffi 34.26% <100.00%> (-2.18%) ⬇️
rpc 19.92% <ø> (ø)
spv 83.74% <ø> (-0.12%) ⬇️
wallet 66.97% <ø> (ø)
Files with missing lines Coverage Δ
dash-spv-ffi/src/config.rs 62.69% <100.00%> (+1.19%) ⬆️

... and 15 files with indirect coverage changes

Tests cover:
- clearing peers after adding multiple peers
- clearing an already-empty peer list succeeds
- null pointer returns NullPointer error code

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@xdustinface xdustinface left a comment

Choose a reason for hiding this comment

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

Lets maybe just instead completely get rid of default_peers_for_network and add the peer where we need it? I don't think this "add the default peer for regtest" behaviour is good. I had similar problems before with this default peer behaviour on regtest.

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.

2 participants