Skip to content

Commit becff66

Browse files
committed
merge FFI callback setup into dash_spv_ffi_client_run
Pass all event callbacks as a single `FFIEventCallbacks` struct directly to `run()` instead of requiring separate set_*_callbacks() calls. This removes 5 Mutex fields from `FFIDashSpvClient` and 10 FFI functions.
1 parent 2eab339 commit becff66

8 files changed

Lines changed: 148 additions & 611 deletions

File tree

dash-spv-ffi/FFI_API.md

Lines changed: 5 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ 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**: 49
7+
**Total Functions**: 39
88

99
## Table of Contents
1010

1111
- [Client Management](#client-management)
1212
- [Configuration](#configuration)
1313
- [Synchronization](#synchronization)
14-
- [Wallet Operations](#wallet-operations)
1514
- [Transaction Management](#transaction-management)
1615
- [Platform Integration](#platform-integration)
17-
- [Event Callbacks](#event-callbacks)
1816
- [Error Handling](#error-handling)
1917
- [Utility Functions](#utility-functions)
2018

@@ -55,25 +53,14 @@ Functions: 16
5553

5654
### Synchronization
5755

58-
Functions: 5
56+
Functions: 3
5957

6058
| Function | Description | Module |
6159
|----------|-------------|--------|
62-
| `dash_spv_ffi_client_clear_sync_event_callbacks` | Clear sync event callbacks | client |
6360
| `dash_spv_ffi_client_get_manager_sync_progress` | Get the current manager-based sync progress | client |
6461
| `dash_spv_ffi_client_get_sync_progress` | Get the current sync progress snapshot | client |
65-
| `dash_spv_ffi_client_set_sync_event_callbacks` | Set sync event callbacks for push-based event notifications | client |
6662
| `dash_spv_ffi_sync_progress_destroy` | Destroy an `FFISyncProgress` object and all its nested pointers | types |
6763

68-
### Wallet Operations
69-
70-
Functions: 2
71-
72-
| Function | Description | Module |
73-
|----------|-------------|--------|
74-
| `dash_spv_ffi_client_clear_wallet_event_callbacks` | Clear wallet event callbacks | client |
75-
| `dash_spv_ffi_client_set_wallet_event_callbacks` | Set wallet event callbacks for push-based event notifications | client |
76-
7764
### Transaction Management
7865

7966
Functions: 1
@@ -91,19 +78,6 @@ Functions: 2
9178
| `ffi_dash_spv_get_platform_activation_height` | Gets the platform activation height from the Core chain # Safety This... | platform_integration |
9279
| `ffi_dash_spv_get_quorum_public_key` | Gets a quorum public key from the Core chain # Safety This function is... | platform_integration |
9380

94-
### Event Callbacks
95-
96-
Functions: 6
97-
98-
| Function | Description | Module |
99-
|----------|-------------|--------|
100-
| `dash_spv_ffi_client_clear_client_error_callback` | Clear the client error callback | client |
101-
| `dash_spv_ffi_client_clear_network_event_callbacks` | Clear network event callbacks | client |
102-
| `dash_spv_ffi_client_clear_progress_callback` | Clear progress callback | client |
103-
| `dash_spv_ffi_client_set_client_error_callback` | Set a callback for fatal client errors (start failure, sync thread crash) | client |
104-
| `dash_spv_ffi_client_set_network_event_callbacks` | Set network event callbacks for push-based event notifications | client |
105-
| `dash_spv_ffi_client_set_progress_callback` | Set progress callback for sync progress updates | client |
106-
10781
### Error Handling
10882

10983
Functions: 1
@@ -426,22 +400,6 @@ dash_spv_ffi_config_testnet() -> *mut FFIClientConfig
426400

427401
### Synchronization - Detailed
428402

429-
#### `dash_spv_ffi_client_clear_sync_event_callbacks`
430-
431-
```c
432-
dash_spv_ffi_client_clear_sync_event_callbacks(client: *mut FFIDashSpvClient,) -> i32
433-
```
434-
435-
**Description:**
436-
Clear sync event callbacks. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
437-
438-
**Safety:**
439-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
440-
441-
**Module:** `client`
442-
443-
---
444-
445403
#### `dash_spv_ffi_client_get_manager_sync_progress`
446404

447405
```c
@@ -474,22 +432,6 @@ Get the current sync progress snapshot. # Safety - `client` must be a valid, no
474432

475433
---
476434

477-
#### `dash_spv_ffi_client_set_sync_event_callbacks`
478-
479-
```c
480-
dash_spv_ffi_client_set_sync_event_callbacks(client: *mut FFIDashSpvClient, callbacks: FFISyncEventCallbacks,) -> i32
481-
```
482-
483-
**Description:**
484-
Set sync event callbacks for push-based event notifications. Call this before calling run(). The callbacks are snapshotted when `dash_spv_ffi_client_run` is called. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callbacks` struct and its `user_data` must remain valid until callbacks are cleared. - Callbacks must be thread-safe as they may be called from a background task.
485-
486-
**Safety:**
487-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callbacks` struct and its `user_data` must remain valid until callbacks are cleared. - Callbacks must be thread-safe as they may be called from a background task.
488-
489-
**Module:** `client`
490-
491-
---
492-
493435
#### `dash_spv_ffi_sync_progress_destroy`
494436

495437
```c
@@ -506,40 +448,6 @@ Destroy an `FFISyncProgress` object and all its nested pointers. # Safety - `pr
506448
507449
---
508450
509-
### Wallet Operations - Detailed
510-
511-
#### `dash_spv_ffi_client_clear_wallet_event_callbacks`
512-
513-
```c
514-
dash_spv_ffi_client_clear_wallet_event_callbacks(client: *mut FFIDashSpvClient,) -> i32
515-
```
516-
517-
**Description:**
518-
Clear wallet event callbacks. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
519-
520-
**Safety:**
521-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
522-
523-
**Module:** `client`
524-
525-
---
526-
527-
#### `dash_spv_ffi_client_set_wallet_event_callbacks`
528-
529-
```c
530-
dash_spv_ffi_client_set_wallet_event_callbacks(client: *mut FFIDashSpvClient, callbacks: FFIWalletEventCallbacks,) -> i32
531-
```
532-
533-
**Description:**
534-
Set wallet event callbacks for push-based event notifications. Call this before calling run(). The callbacks are snapshotted when `dash_spv_ffi_client_run` is called. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callbacks` struct and its `user_data` must remain valid until callbacks are cleared. - Callbacks must be thread-safe as they may be called from a background task.
535-
536-
**Safety:**
537-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callbacks` struct and its `user_data` must remain valid until callbacks are cleared. - Callbacks must be thread-safe as they may be called from a background task.
538-
539-
**Module:** `client`
540-
541-
---
542-
543451
### Transaction Management - Detailed
544452
545453
#### `dash_spv_ffi_client_broadcast_transaction`
@@ -592,104 +500,6 @@ This function is unsafe because: - The caller must ensure all pointers are valid
592500

593501
---
594502

595-
### Event Callbacks - Detailed
596-
597-
#### `dash_spv_ffi_client_clear_client_error_callback`
598-
599-
```c
600-
dash_spv_ffi_client_clear_client_error_callback(client: *mut FFIDashSpvClient,) -> i32
601-
```
602-
603-
**Description:**
604-
Clear the client error callback. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
605-
606-
**Safety:**
607-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
608-
609-
**Module:** `client`
610-
611-
---
612-
613-
#### `dash_spv_ffi_client_clear_network_event_callbacks`
614-
615-
```c
616-
dash_spv_ffi_client_clear_network_event_callbacks(client: *mut FFIDashSpvClient,) -> i32
617-
```
618-
619-
**Description:**
620-
Clear network event callbacks. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
621-
622-
**Safety:**
623-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
624-
625-
**Module:** `client`
626-
627-
---
628-
629-
#### `dash_spv_ffi_client_clear_progress_callback`
630-
631-
```c
632-
dash_spv_ffi_client_clear_progress_callback(client: *mut FFIDashSpvClient,) -> i32
633-
```
634-
635-
**Description:**
636-
Clear progress callback. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
637-
638-
**Safety:**
639-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`.
640-
641-
**Module:** `client`
642-
643-
---
644-
645-
#### `dash_spv_ffi_client_set_client_error_callback`
646-
647-
```c
648-
dash_spv_ffi_client_set_client_error_callback(client: *mut FFIDashSpvClient, callback: FFIClientErrorCallback,) -> i32
649-
```
650-
651-
**Description:**
652-
Set a callback for fatal client errors (start failure, sync thread crash). # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callback` struct and its `user_data` must remain valid until the callback is cleared. - The callback must be thread-safe as it may be called from a background thread.
653-
654-
**Safety:**
655-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callback` struct and its `user_data` must remain valid until the callback is cleared. - The callback must be thread-safe as it may be called from a background thread.
656-
657-
**Module:** `client`
658-
659-
---
660-
661-
#### `dash_spv_ffi_client_set_network_event_callbacks`
662-
663-
```c
664-
dash_spv_ffi_client_set_network_event_callbacks(client: *mut FFIDashSpvClient, callbacks: FFINetworkEventCallbacks,) -> i32
665-
```
666-
667-
**Description:**
668-
Set network event callbacks for push-based event notifications. Call this before calling run(). The callbacks are snapshotted when `dash_spv_ffi_client_run` is called. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callbacks` struct and its `user_data` must remain valid until callbacks are cleared. - Callbacks must be thread-safe as they may be called from a background task.
669-
670-
**Safety:**
671-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callbacks` struct and its `user_data` must remain valid until callbacks are cleared. - Callbacks must be thread-safe as they may be called from a background task.
672-
673-
**Module:** `client`
674-
675-
---
676-
677-
#### `dash_spv_ffi_client_set_progress_callback`
678-
679-
```c
680-
dash_spv_ffi_client_set_progress_callback(client: *mut FFIDashSpvClient, callback: crate::FFIProgressCallback,) -> i32
681-
```
682-
683-
**Description:**
684-
Set progress callback for sync progress updates. Call this before calling run(). The callback is snapshotted when `dash_spv_ffi_client_run` is called. # Safety - `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callback` struct and its `user_data` must remain valid until the callback is cleared. - The callback must be thread-safe as it may be called from a background task.
685-
686-
**Safety:**
687-
- `client` must be a valid, non-null pointer to an `FFIDashSpvClient`. - The `callback` struct and its `user_data` must remain valid until the callback is cleared. - The callback must be thread-safe as it may be called from a background task.
688-
689-
**Module:** `client`
690-
691-
---
692-
693503
### Error Handling - Detailed
694504

695505
#### `dash_spv_ffi_get_last_error`
@@ -787,14 +597,14 @@ The caller must ensure that: - The client pointer is valid - The returned pointe
787597
#### `dash_spv_ffi_client_run`
788598
789599
```c
790-
dash_spv_ffi_client_run(client: *mut FFIDashSpvClient) -> i32
600+
dash_spv_ffi_client_run(client: *mut FFIDashSpvClient, callbacks: FFIEventCallbacks,) -> i32
791601
```
792602

793603
**Description:**
794-
Start the SPV client and begin syncing in the background. Creates an event handler from the currently configured callbacks and passes it to the library's `run()` method, which manages all channel subscriptions and monitoring tasks internally. Returns immediately after spawning. Configure callbacks before calling `run()` via `set_sync_event_callbacks`, `set_network_event_callbacks`, `set_wallet_event_callbacks`, etc. # Safety - `client` must be a valid, non-null pointer to a created client. # Returns 0 on success, error code on failure.
604+
Start the SPV client and begin syncing in the background. Accepts all event callbacks as a single struct and passes them to the library's `run()` method, which manages channel subscriptions and monitoring tasks internally. Returns immediately after spawning. # Safety - `client` must be a valid, non-null pointer to a created client. - The `callbacks` struct and its `user_data` pointers must remain valid until `dash_spv_ffi_client_stop` or `dash_spv_ffi_client_destroy` is called. # Returns 0 on success, error code on failure.
795605

796606
**Safety:**
797-
- `client` must be a valid, non-null pointer to a created client.
607+
- `client` must be a valid, non-null pointer to a created client. - The `callbacks` struct and its `user_data` pointers must remain valid until `dash_spv_ffi_client_stop` or `dash_spv_ffi_client_destroy` is called.
798608

799609
**Module:** `client`
800610

dash-spv-ffi/src/bin/ffi_cli.rs

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -403,84 +403,50 @@ fn main() {
403403
dash_spv_ffi_wallet_manager_free(wallet_manager);
404404
}
405405

406-
// Set up event callbacks
407-
let sync_callbacks = FFISyncEventCallbacks {
408-
on_sync_start: Some(on_sync_start),
409-
on_block_headers_stored: Some(on_block_headers_stored),
410-
on_block_header_sync_complete: Some(on_block_header_sync_complete),
411-
on_filter_headers_stored: Some(on_filter_headers_stored),
412-
on_filter_headers_sync_complete: Some(on_filter_headers_sync_complete),
413-
on_filters_stored: Some(on_filters_stored),
414-
on_filters_sync_complete: Some(on_filters_sync_complete),
415-
on_blocks_needed: Some(on_blocks_needed),
416-
on_block_processed: Some(on_block_processed),
417-
on_masternode_state_updated: Some(on_masternode_state_updated),
418-
on_chainlock_received: Some(on_chainlock_received),
419-
on_instantlock_received: Some(on_instantlock_received),
420-
on_manager_error: Some(on_manager_error),
421-
on_sync_complete: Some(on_sync_complete),
422-
user_data: ptr::null_mut(),
406+
// Build all event callbacks in a single struct
407+
let callbacks = FFIEventCallbacks {
408+
sync: FFISyncEventCallbacks {
409+
on_sync_start: Some(on_sync_start),
410+
on_block_headers_stored: Some(on_block_headers_stored),
411+
on_block_header_sync_complete: Some(on_block_header_sync_complete),
412+
on_filter_headers_stored: Some(on_filter_headers_stored),
413+
on_filter_headers_sync_complete: Some(on_filter_headers_sync_complete),
414+
on_filters_stored: Some(on_filters_stored),
415+
on_filters_sync_complete: Some(on_filters_sync_complete),
416+
on_blocks_needed: Some(on_blocks_needed),
417+
on_block_processed: Some(on_block_processed),
418+
on_masternode_state_updated: Some(on_masternode_state_updated),
419+
on_chainlock_received: Some(on_chainlock_received),
420+
on_instantlock_received: Some(on_instantlock_received),
421+
on_manager_error: Some(on_manager_error),
422+
on_sync_complete: Some(on_sync_complete),
423+
user_data: ptr::null_mut(),
424+
},
425+
network: FFINetworkEventCallbacks {
426+
on_peer_connected: Some(on_peer_connected),
427+
on_peer_disconnected: Some(on_peer_disconnected),
428+
on_peers_updated: Some(on_peers_updated),
429+
user_data: ptr::null_mut(),
430+
},
431+
progress: FFIProgressCallback {
432+
on_progress: Some(on_progress_update),
433+
user_data: ptr::null_mut(),
434+
},
435+
wallet: FFIWalletEventCallbacks {
436+
on_transaction_received: Some(on_transaction_received),
437+
on_balance_updated: Some(on_balance_updated),
438+
user_data: ptr::null_mut(),
439+
},
440+
error: FFIClientErrorCallback {
441+
on_error: None,
442+
user_data: ptr::null_mut(),
443+
},
423444
};
424445

425-
let network_callbacks = FFINetworkEventCallbacks {
426-
on_peer_connected: Some(on_peer_connected),
427-
on_peer_disconnected: Some(on_peer_disconnected),
428-
on_peers_updated: Some(on_peers_updated),
429-
user_data: ptr::null_mut(),
430-
};
431-
432-
let wallet_callbacks = FFIWalletEventCallbacks {
433-
on_transaction_received: Some(on_transaction_received),
434-
on_balance_updated: Some(on_balance_updated),
435-
user_data: ptr::null_mut(),
436-
};
437-
438-
let rc = dash_spv_ffi_client_set_sync_event_callbacks(client, sync_callbacks);
439-
if rc != FFIErrorCode::Success as i32 {
440-
eprintln!(
441-
"Failed to set sync callbacks: {}",
442-
ffi_string_to_rust(dash_spv_ffi_get_last_error())
443-
);
444-
std::process::exit(1);
445-
}
446-
447-
let rc = dash_spv_ffi_client_set_network_event_callbacks(client, network_callbacks);
448-
if rc != FFIErrorCode::Success as i32 {
449-
eprintln!(
450-
"Failed to set network callbacks: {}",
451-
ffi_string_to_rust(dash_spv_ffi_get_last_error())
452-
);
453-
std::process::exit(1);
454-
}
455-
456-
let rc = dash_spv_ffi_client_set_wallet_event_callbacks(client, wallet_callbacks);
457-
if rc != FFIErrorCode::Success as i32 {
458-
eprintln!(
459-
"Failed to set wallet callbacks: {}",
460-
ffi_string_to_rust(dash_spv_ffi_get_last_error())
461-
);
462-
std::process::exit(1);
463-
}
464-
465-
// Set up progress callback
466-
let progress_callback = FFIProgressCallback {
467-
on_progress: Some(on_progress_update),
468-
user_data: ptr::null_mut(),
469-
};
470-
471-
let rc = dash_spv_ffi_client_set_progress_callback(client, progress_callback);
472-
if rc != FFIErrorCode::Success as i32 {
473-
eprintln!(
474-
"Failed to set progress callback: {}",
475-
ffi_string_to_rust(dash_spv_ffi_get_last_error())
476-
);
477-
std::process::exit(1);
478-
}
479-
480446
println!("Event and progress callbacks configured, starting sync...");
481447

482448
// Run client - starts sync in background and returns immediately
483-
let rc = dash_spv_ffi_client_run(client);
449+
let rc = dash_spv_ffi_client_run(client, callbacks);
484450
if rc != FFIErrorCode::Success as i32 {
485451
eprintln!("Client run failed: {}", ffi_string_to_rust(dash_spv_ffi_get_last_error()));
486452
std::process::exit(1);

0 commit comments

Comments
 (0)