Skip to content

Commit 472131a

Browse files
feat(ffi): add dash_spv_ffi_config_clear_peers (#591)
* feat(ffi): add dash_spv_ffi_config_clear_peers to remove default peers 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> * chore: regenerate FFI docs and apply fmt Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test: add unit tests for dash_spv_ffi_config_clear_peers 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> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fcdffa7 commit 472131a

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

dash-spv-ffi/FFI_API.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I
44

55
**Auto-generated**: This documentation is automatically generated from the source code. Do not edit manually.
66

7-
**Total Functions**: 40
7+
**Total Functions**: 41
88

99
## Table of Contents
1010

@@ -31,12 +31,13 @@ Functions: 3
3131

3232
### Configuration
3333

34-
Functions: 16
34+
Functions: 17
3535

3636
| Function | Description | Module |
3737
|----------|-------------|--------|
3838
| `dash_spv_ffi_client_update_config` | Update the running client's configuration | client |
3939
| `dash_spv_ffi_config_add_peer` | Adds a peer address to the configuration Accepts socket addresses with or... | config |
40+
| `dash_spv_ffi_config_clear_peers` | Removes all configured peers from the configuration This is useful when the... | config |
4041
| `dash_spv_ffi_config_destroy` | Destroys an FFIClientConfig and frees its memory # Safety - `config` must... | config |
4142
| `dash_spv_ffi_config_get_network` | Gets the network type from the configuration # Safety - `config` must be a... | config |
4243
| `dash_spv_ffi_config_mainnet` | No description | config |
@@ -201,6 +202,22 @@ Adds a peer address to the configuration Accepts socket addresses with or witho
201202
202203
---
203204
205+
#### `dash_spv_ffi_config_clear_peers`
206+
207+
```c
208+
dash_spv_ffi_config_clear_peers(config: *mut FFIClientConfig) -> i32
209+
```
210+
211+
**Description:**
212+
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`. # Safety - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet - The caller must ensure the config pointer remains valid for the duration of this call
213+
214+
**Safety:**
215+
- `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet - The caller must ensure the config pointer remains valid for the duration of this call
216+
217+
**Module:** `config`
218+
219+
---
220+
204221
#### `dash_spv_ffi_config_destroy`
205222

206223
```c

dash-spv-ffi/src/config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ pub unsafe extern "C" fn dash_spv_ffi_config_add_peer(
166166
}
167167
}
168168

169+
/// Removes all configured peers from the configuration
170+
///
171+
/// This is useful when the caller wants to start with a clean slate
172+
/// before adding custom peers via `dash_spv_ffi_config_add_peer`.
173+
///
174+
/// # Safety
175+
/// - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
176+
/// - The caller must ensure the config pointer remains valid for the duration of this call
177+
#[no_mangle]
178+
pub unsafe extern "C" fn dash_spv_ffi_config_clear_peers(config: *mut FFIClientConfig) -> i32 {
179+
null_check!(config);
180+
181+
let cfg = unsafe { &mut *((*config).inner as *mut ClientConfig) };
182+
cfg.peers.clear();
183+
FFIErrorCode::Success as i32
184+
}
185+
169186
/// Sets the user agent string to advertise in the P2P handshake
170187
///
171188
/// # Safety

dash-spv-ffi/tests/test_config.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,39 @@ mod tests {
9494
}
9595
}
9696

97+
#[test]
98+
#[serial]
99+
fn test_config_clear_peers() {
100+
unsafe {
101+
let config = dash_spv_ffi_config_new(FFINetwork::Testnet);
102+
103+
// Add peers
104+
let peer1 = CString::new("127.0.0.1:9999").unwrap();
105+
let peer2 = CString::new("127.0.0.2:9999").unwrap();
106+
dash_spv_ffi_config_add_peer(config, peer1.as_ptr());
107+
dash_spv_ffi_config_add_peer(config, peer2.as_ptr());
108+
109+
// Clear all peers
110+
let result = dash_spv_ffi_config_clear_peers(config);
111+
assert_eq!(result, FFIErrorCode::Success as i32);
112+
113+
// Clear on already-empty should still succeed
114+
let result = dash_spv_ffi_config_clear_peers(config);
115+
assert_eq!(result, FFIErrorCode::Success as i32);
116+
117+
dash_spv_ffi_config_destroy(config);
118+
}
119+
}
120+
121+
#[test]
122+
#[serial]
123+
fn test_config_clear_peers_null() {
124+
unsafe {
125+
let result = dash_spv_ffi_config_clear_peers(std::ptr::null_mut());
126+
assert_eq!(result, FFIErrorCode::NullPointer as i32);
127+
}
128+
}
129+
97130
#[test]
98131
#[serial]
99132
fn test_config_user_agent() {

0 commit comments

Comments
 (0)