Skip to content
Draft
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
29 changes: 28 additions & 1 deletion 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**: 49
**Total Functions**: 50

## Table of Contents

Expand All @@ -13,6 +13,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I
- [Synchronization](#synchronization)
- [Wallet Operations](#wallet-operations)
- [Transaction Management](#transaction-management)
- [Mempool Operations](#mempool-operations)
- [Platform Integration](#platform-integration)
- [Event Callbacks](#event-callbacks)
- [Error Handling](#error-handling)
Expand Down Expand Up @@ -82,6 +83,14 @@ Functions: 1
|----------|-------------|--------|
| `dash_spv_ffi_client_broadcast_transaction` | Broadcasts a transaction to the Dash network via connected peers | client |

### Mempool Operations

Functions: 1

| Function | Description | Module |
|----------|-------------|--------|
| `dash_spv_ffi_mempool_progress_destroy` | Destroy an `FFIMempoolProgress` object | types |

### Platform Integration

Functions: 2
Expand Down Expand Up @@ -558,6 +567,24 @@ Broadcasts a transaction to the Dash network via connected peers. # Safety - `

---

### Mempool Operations - Detailed

#### `dash_spv_ffi_mempool_progress_destroy`

```c
dash_spv_ffi_mempool_progress_destroy(progress: *mut FFIMempoolProgress) -> ()
```

**Description:**
Destroy an `FFIMempoolProgress` object. # Safety - `progress` must be a pointer returned from this crate, or null.

**Safety:**
- `progress` must be a pointer returned from this crate, or null.

**Module:** `types`

---

### Platform Integration - Detailed

#### `ffi_dash_spv_get_platform_activation_height`
Expand Down
24 changes: 24 additions & 0 deletions dash-spv-ffi/include/dash_spv_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef enum FFIManagerId {
Masternodes = 4,
ChainLocks = 5,
InstantSend = 6,
Mempool = 7,
} FFIManagerId;

typedef enum FFIMempoolStrategy {
Expand Down Expand Up @@ -144,6 +145,18 @@ typedef struct FFIInstantSendProgress {
uint64_t last_activity;
} FFIInstantSendProgress;

/**
* Progress for mempool transaction monitoring.
*/
typedef struct FFIMempoolProgress {
enum FFISyncState state;
uint32_t received;
uint32_t relevant;
uint32_t tracked;
uint32_t removed;
uint64_t last_activity;
} FFIMempoolProgress;

/**
* Aggregate progress for all sync managers.
* Provides a complete view of the parallel sync system's state.
Expand All @@ -162,6 +175,7 @@ typedef struct FFISyncProgress {
struct FFIMasternodesProgress *masternodes;
struct FFIChainLockProgress *chainlocks;
struct FFIInstantSendProgress *instantsend;
struct FFIMempoolProgress *mempool;
} FFISyncProgress;

/**
Expand Down Expand Up @@ -249,6 +263,8 @@ typedef void (*OnBlocksNeededCallback)(const struct FFIBlockNeeded *blocks,
typedef void (*OnBlockProcessedCallback)(uint32_t height,
const uint8_t (*hash)[32],
uint32_t new_address_count,
const uint8_t (*confirmed_txids)[32],
uint32_t confirmed_txid_count,
void *user_data);

/**
Expand Down Expand Up @@ -976,6 +992,14 @@ struct FFIResult ffi_dash_spv_get_platform_activation_height(struct FFIDashSpvCl
*/
void dash_spv_ffi_instantsend_progress_destroy(struct FFIInstantSendProgress *progress) ;

/**
* Destroy an `FFIMempoolProgress` object.
*
* # Safety
* - `progress` must be a pointer returned from this crate, or null.
*/
void dash_spv_ffi_mempool_progress_destroy(struct FFIMempoolProgress *progress) ;

/**
* Destroy an `FFISyncProgress` object and all its nested pointers.
*
Expand Down
8 changes: 7 additions & 1 deletion dash-spv-ffi/src/bin/ffi_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" fn on_sync_start(manager_id: FFIManagerId, _user_data: *mut c_void) {
FFIManagerId::Masternodes => "Masternodes",
FFIManagerId::ChainLocks => "ChainLocks",
FFIManagerId::InstantSend => "InstantSend",
FFIManagerId::Mempool => "Mempool",
};
println!("[Sync] Manager started: {}", manager_name);
}
Expand Down Expand Up @@ -76,9 +77,14 @@ extern "C" fn on_block_processed(
height: u32,
_hash: *const [u8; 32],
new_address_count: u32,
_confirmed_txids: *const [u8; 32],
confirmed_txid_count: u32,
_user_data: *mut c_void,
) {
println!("[Sync] Block processed: height={}, new_addresses={}", height, new_address_count);
println!(
"[Sync] Block processed: height={}, new_addresses={}, confirmed_txs={}",
height, new_address_count, confirmed_txid_count
);
}

extern "C" fn on_masternode_state_updated(height: u32, _user_data: *mut c_void) {
Expand Down
9 changes: 9 additions & 0 deletions dash-spv-ffi/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum FFIManagerId {
Masternodes = 4,
ChainLocks = 5,
InstantSend = 6,
Mempool = 7,
}

impl From<dash_spv::sync::ManagerIdentifier> for FFIManagerId {
Expand All @@ -40,6 +41,7 @@ impl From<dash_spv::sync::ManagerIdentifier> for FFIManagerId {
dash_spv::sync::ManagerIdentifier::Masternode => FFIManagerId::Masternodes,
dash_spv::sync::ManagerIdentifier::ChainLock => FFIManagerId::ChainLocks,
dash_spv::sync::ManagerIdentifier::InstantSend => FFIManagerId::InstantSend,
dash_spv::sync::ManagerIdentifier::Mempool => FFIManagerId::Mempool,
}
}
}
Expand Down Expand Up @@ -163,6 +165,8 @@ pub type OnBlockProcessedCallback = Option<
height: u32,
hash: *const [u8; 32],
new_address_count: u32,
confirmed_txids: *const [u8; 32],
confirmed_txid_count: u32,
user_data: *mut c_void,
),
>;
Expand Down Expand Up @@ -351,13 +355,18 @@ impl FFISyncEventCallbacks {
block_hash,
height,
new_addresses,
confirmed_txids,
} => {
if let Some(cb) = self.on_block_processed {
let hash_bytes = block_hash.as_byte_array();
let txid_bytes: Vec<[u8; 32]> =
confirmed_txids.iter().map(|txid| *txid.as_byte_array()).collect();
cb(
*height,
hash_bytes as *const [u8; 32],
new_addresses.len() as u32,
txid_bytes.as_ptr(),
txid_bytes.len() as u32,
self.user_data,
);
}
Expand Down
51 changes: 49 additions & 2 deletions dash-spv-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dash_spv::client::config::MempoolStrategy;
use dash_spv::sync::{
BlockHeadersProgress, BlocksProgress, ChainLockProgress, FilterHeadersProgress,
FiltersProgress, InstantSendProgress, MasternodesProgress, ProgressPercentage, SyncProgress,
SyncState,
FiltersProgress, InstantSendProgress, MasternodesProgress, MempoolProgress, ProgressPercentage,
SyncProgress, SyncState,
};
use dash_spv::types::MempoolRemovalReason;
use std::ffi::{CStr, CString};
Expand Down Expand Up @@ -259,6 +259,31 @@ impl From<&InstantSendProgress> for FFIInstantSendProgress {
}
}

/// Progress for mempool transaction monitoring.
#[repr(C)]
#[derive(Debug, Clone, Default)]
pub struct FFIMempoolProgress {
pub state: FFISyncState,
pub received: u32,
pub relevant: u32,
pub tracked: u32,
pub removed: u32,
pub last_activity: u64,
}

impl From<&MempoolProgress> for FFIMempoolProgress {
fn from(progress: &MempoolProgress) -> Self {
FFIMempoolProgress {
state: progress.state().into(),
received: progress.received(),
relevant: progress.relevant(),
tracked: progress.tracked(),
removed: progress.removed(),
last_activity: progress.last_activity().elapsed().as_secs(),
}
}
}

/// Aggregate progress for all sync managers.
/// Provides a complete view of the parallel sync system's state.
#[repr(C)]
Expand All @@ -274,6 +299,7 @@ pub struct FFISyncProgress {
pub masternodes: *mut FFIMasternodesProgress,
pub chainlocks: *mut FFIChainLockProgress,
pub instantsend: *mut FFIInstantSendProgress,
pub mempool: *mut FFIMempoolProgress,
}

impl From<SyncProgress> for FFISyncProgress {
Expand Down Expand Up @@ -320,6 +346,12 @@ impl From<SyncProgress> for FFISyncProgress {
.map(|p| Box::into_raw(Box::new(FFIInstantSendProgress::from(p))))
.unwrap_or(std::ptr::null_mut());

let mempool = progress
.mempool()
.ok()
.map(|p| Box::into_raw(Box::new(FFIMempoolProgress::from(p))))
.unwrap_or(std::ptr::null_mut());

Self {
state: progress.state().into(),
percentage: progress.percentage(),
Expand All @@ -331,6 +363,7 @@ impl From<SyncProgress> for FFISyncProgress {
masternodes,
chainlocks,
instantsend,
mempool,
}
}
}
Expand Down Expand Up @@ -486,6 +519,17 @@ pub unsafe extern "C" fn dash_spv_ffi_instantsend_progress_destroy(
}
}

/// Destroy an `FFIMempoolProgress` object.
///
/// # Safety
/// - `progress` must be a pointer returned from this crate, or null.
#[no_mangle]
pub unsafe extern "C" fn dash_spv_ffi_mempool_progress_destroy(progress: *mut FFIMempoolProgress) {
if !progress.is_null() {
let _ = Box::from_raw(progress);
}
}

/// Destroy an `FFISyncProgress` object and all its nested pointers.
///
/// # Safety
Expand Down Expand Up @@ -517,5 +561,8 @@ pub unsafe extern "C" fn dash_spv_ffi_sync_progress_destroy(progress: *mut FFISy
if !p.instantsend.is_null() {
dash_spv_ffi_instantsend_progress_destroy(p.instantsend);
}
if !p.mempool.is_null() {
dash_spv_ffi_mempool_progress_destroy(p.mempool);
}
}
}
9 changes: 8 additions & 1 deletion dash-spv-ffi/tests/dashd_sync/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,21 @@ extern "C" fn on_block_processed(
height: u32,
_hash: *const [u8; 32],
new_address_count: u32,
_confirmed_txids: *const [u8; 32],
confirmed_txid_count: u32,
user_data: *mut c_void,
) {
let Some(tracker) = (unsafe { tracker_from(user_data) }) else {
return;
};
tracker.processed_block_heights.lock().unwrap_or_else(|e| e.into_inner()).push(height);
tracker.block_processed_count.fetch_add(1, Ordering::SeqCst);
tracing::debug!("on_block_processed: height={}, new_addresses={}", height, new_address_count);
tracing::debug!(
"on_block_processed: height={}, new_addresses={}, confirmed_txs={}",
height,
new_address_count,
confirmed_txid_count
);
}

extern "C" fn on_masternode_state_updated(height: u32, user_data: *mut c_void) {
Expand Down
Loading
Loading