Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions dash-spv-ffi/FFI_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I

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

**Total Functions**: 40
**Total Functions**: 41

## Table of Contents

Expand All @@ -31,12 +31,13 @@ Functions: 3

### Configuration

Functions: 16
Functions: 17

| Function | Description | Module |
|----------|-------------|--------|
| `dash_spv_ffi_client_update_config` | Update the running client's configuration | client |
| `dash_spv_ffi_config_add_peer` | Adds a peer address to the configuration Accepts socket addresses with or... | config |
| `dash_spv_ffi_config_clear_peers` | Removes all configured peers from the configuration This is useful when the... | config |
| `dash_spv_ffi_config_destroy` | Destroys an FFIClientConfig and frees its memory # Safety - `config` must... | config |
| `dash_spv_ffi_config_get_network` | Gets the network type from the configuration # Safety - `config` must be a... | config |
| `dash_spv_ffi_config_mainnet` | No description | config |
Expand Down Expand Up @@ -201,6 +202,22 @@ Adds a peer address to the configuration Accepts socket addresses with or witho

---

#### `dash_spv_ffi_config_clear_peers`

```c
dash_spv_ffi_config_clear_peers(config: *mut FFIClientConfig) -> i32
```

**Description:**
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

**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

**Module:** `config`

---

#### `dash_spv_ffi_config_destroy`

```c
Expand Down
17 changes: 17 additions & 0 deletions dash-spv-ffi/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ pub unsafe extern "C" fn dash_spv_ffi_config_add_peer(
}
}

/// 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
#[no_mangle]
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
}
Comment on lines +178 to +184
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.


/// Sets the user agent string to advertise in the P2P handshake
///
/// # Safety
Expand Down
33 changes: 33 additions & 0 deletions dash-spv-ffi/tests/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ mod tests {
}
}

#[test]
#[serial]
fn test_config_clear_peers() {
unsafe {
let config = dash_spv_ffi_config_new(FFINetwork::Testnet);

// Add peers
let peer1 = CString::new("127.0.0.1:9999").unwrap();
let peer2 = CString::new("127.0.0.2:9999").unwrap();
dash_spv_ffi_config_add_peer(config, peer1.as_ptr());
dash_spv_ffi_config_add_peer(config, peer2.as_ptr());

// Clear all peers
let result = dash_spv_ffi_config_clear_peers(config);
assert_eq!(result, FFIErrorCode::Success as i32);

// Clear on already-empty should still succeed
let result = dash_spv_ffi_config_clear_peers(config);
assert_eq!(result, FFIErrorCode::Success as i32);

dash_spv_ffi_config_destroy(config);
}
}

#[test]
#[serial]
fn test_config_clear_peers_null() {
unsafe {
let result = dash_spv_ffi_config_clear_peers(std::ptr::null_mut());
assert_eq!(result, FFIErrorCode::NullPointer as i32);
}
}

#[test]
#[serial]
fn test_config_user_agent() {
Expand Down
Loading