Skip to content

Commit c8dbac7

Browse files
more work
1 parent a8c0697 commit c8dbac7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2022
-323
lines changed

dash-spv-ffi/FFI_API.md

Lines changed: 140 additions & 20 deletions
Large diffs are not rendered by default.

dash-spv-ffi/cbindgen.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ exclude = ["Option_BlockCallback", "Option_TransactionCallback", "Option_Balance
1414
prefix = "dash_spv_ffi_"
1515

1616
[export.rename]
17-
"FFINetwork" = "DashSpvNetwork"
1817
"FFIValidationMode" = "DashSpvValidationMode"
1918
"FFIErrorCode" = "DashSpvErrorCode"
2019

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
/// Example demonstrating the simplified FFIWalletManager usage
2-
///
2+
///
33
/// The refactored design removes unnecessary indirection by:
44
/// 1. FFIWalletManager directly contains Arc<RwLock<WalletManager>>
55
/// 2. No longer requires going through the client for each operation
66
/// 3. Cleaner and more efficient access to wallet functionality
7-
87
use dash_spv_ffi::*;
9-
use key_wallet_ffi::{
10-
wallet_manager_wallet_count,
11-
wallet_manager_free,
12-
FFIError,
13-
};
8+
use key_wallet_ffi::{wallet_manager_free, wallet_manager_wallet_count, FFIError};
149

1510
fn main() {
1611
unsafe {
@@ -19,13 +14,13 @@ fn main() {
1914
if config.is_null() {
2015
panic!("Failed to create config");
2116
}
22-
17+
2318
// Create an SPV client
2419
let client = dash_spv_ffi_client_new(config);
2520
if client.is_null() {
2621
panic!("Failed to create client");
2722
}
28-
23+
2924
// Get the wallet manager - now returns void* for Swift compatibility
3025
// This contains a cloned Arc to the wallet manager, allowing
3126
// direct interaction without going through the client
@@ -35,19 +30,19 @@ fn main() {
3530
}
3631
// Cast back to FFIWalletManager for use
3732
let wallet_manager = wallet_manager_ptr as *mut key_wallet_ffi::FFIWalletManager;
38-
33+
3934
// Now we can use the wallet manager directly
4035
// No need to go through client -> inner -> spv_client -> wallet()
41-
36+
4237
// Get the number of wallets (should be 0 initially)
4338
let mut error = std::mem::zeroed::<FFIError>();
4439
let wallet_count = wallet_manager_wallet_count(wallet_manager, &mut error);
4540
println!("Number of wallets: {}", wallet_count);
46-
41+
4742
// Note: To get total balance, you would need to iterate through wallets
4843
// For now, just show the wallet count
4944
println!("Currently managing {} wallets", wallet_count);
50-
45+
5146
// Example of processing a transaction (with mock data)
5247
// In real usage, you would have actual transaction hex
5348
/*
@@ -60,21 +55,21 @@ fn main() {
6055
100000, // block height
6156
&mut error
6257
);
63-
58+
6459
if affected >= 0 {
6560
println!("Transaction affected {} wallets", affected);
6661
} else {
6762
println!("Failed to process transaction");
6863
}
6964
*/
70-
65+
7166
// Clean up
7267
// The wallet manager can now be independently destroyed
7368
// It maintains its own Arc reference to the underlying wallet
7469
wallet_manager_free(wallet_manager);
7570
dash_spv_ffi_client_destroy(client);
7671
dash_spv_ffi_config_destroy(config);
77-
72+
7873
println!("Example completed successfully!");
7974
}
80-
}
75+
}

dash-spv-ffi/include/dash_spv_ffi.h

Lines changed: 171 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ typedef enum FFIMempoolStrategy {
99
Selective = 2,
1010
} FFIMempoolStrategy;
1111

12-
typedef enum FFINetwork {
13-
Dash = 0,
14-
Testnet = 1,
15-
Regtest = 2,
16-
Devnet = 3,
17-
} FFINetwork;
18-
1912
typedef enum FFISyncStage {
2013
Connecting = 0,
2114
QueryingHeight = 1,
@@ -342,54 +335,211 @@ int32_t dash_spv_ffi_client_record_send(struct FFIDashSpvClient *client, const c
342335
*/
343336
void *dash_spv_ffi_client_get_wallet_manager(struct FFIDashSpvClient *client);
344337

345-
FFIClientConfig *dash_spv_ffi_config_new(enum FFINetwork network);
338+
FFIClientConfig *dash_spv_ffi_config_new(FFINetwork network);
346339

347340
FFIClientConfig *dash_spv_ffi_config_mainnet(void);
348341

349342
FFIClientConfig *dash_spv_ffi_config_testnet(void);
350343

351-
int32_t dash_spv_ffi_config_set_data_dir(FFIClientConfig *config, const char *path);
344+
/**
345+
* Sets the data directory for storing blockchain data
346+
*
347+
* # Safety
348+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
349+
* - `path` must be a valid null-terminated C string
350+
* - The caller must ensure the config pointer remains valid for the duration of this call
351+
*/
352+
int32_t dash_spv_ffi_config_set_data_dir(FFIClientConfig *config,
353+
const char *path);
352354

355+
/**
356+
* Sets the validation mode for the SPV client
357+
*
358+
* # Safety
359+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
360+
* - The caller must ensure the config pointer remains valid for the duration of this call
361+
*/
353362
int32_t dash_spv_ffi_config_set_validation_mode(FFIClientConfig *config,
354363
enum FFIValidationMode mode);
355364

356-
int32_t dash_spv_ffi_config_set_max_peers(FFIClientConfig *config, uint32_t max_peers);
365+
/**
366+
* Sets the maximum number of peers to connect to
367+
*
368+
* # Safety
369+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
370+
* - The caller must ensure the config pointer remains valid for the duration of this call
371+
*/
372+
int32_t dash_spv_ffi_config_set_max_peers(FFIClientConfig *config,
373+
uint32_t max_peers);
357374

358-
int32_t dash_spv_ffi_config_add_peer(FFIClientConfig *config, const char *addr);
375+
/**
376+
* Adds a peer address to the configuration
377+
*
378+
* # Safety
379+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
380+
* - `addr` must be a valid null-terminated C string containing a socket address (e.g., "192.168.1.1:9999")
381+
* - The caller must ensure both pointers remain valid for the duration of this call
382+
*/
383+
int32_t dash_spv_ffi_config_add_peer(FFIClientConfig *config,
384+
const char *addr);
359385

360-
int32_t dash_spv_ffi_config_set_user_agent(FFIClientConfig *config, const char *user_agent);
386+
/**
387+
* Sets the user agent string (currently not supported)
388+
*
389+
* # Safety
390+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
391+
* - `user_agent` must be a valid null-terminated C string
392+
* - The caller must ensure both pointers remain valid for the duration of this call
393+
*/
394+
int32_t dash_spv_ffi_config_set_user_agent(FFIClientConfig *config,
395+
const char *user_agent);
361396

362-
int32_t dash_spv_ffi_config_set_relay_transactions(FFIClientConfig *config, bool _relay);
397+
/**
398+
* Sets whether to relay transactions (currently a no-op)
399+
*
400+
* # Safety
401+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
402+
* - The caller must ensure the config pointer remains valid for the duration of this call
403+
*/
404+
int32_t dash_spv_ffi_config_set_relay_transactions(FFIClientConfig *config,
405+
bool _relay);
363406

364-
int32_t dash_spv_ffi_config_set_filter_load(FFIClientConfig *config, bool load_filters);
407+
/**
408+
* Sets whether to load bloom filters
409+
*
410+
* # Safety
411+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
412+
* - The caller must ensure the config pointer remains valid for the duration of this call
413+
*/
414+
int32_t dash_spv_ffi_config_set_filter_load(FFIClientConfig *config,
415+
bool load_filters);
365416

366-
enum FFINetwork dash_spv_ffi_config_get_network(const FFIClientConfig *config);
417+
/**
418+
* Gets the network type from the configuration
419+
*
420+
* # Safety
421+
* - `config` must be a valid pointer to an FFIClientConfig or null
422+
* - If null, returns FFINetwork::Dash as default
423+
*/
424+
FFINetwork dash_spv_ffi_config_get_network(const FFIClientConfig *config);
367425

426+
/**
427+
* Gets the data directory path from the configuration
428+
*
429+
* # Safety
430+
* - `config` must be a valid pointer to an FFIClientConfig or null
431+
* - If null or no data directory is set, returns an FFIString with null pointer
432+
* - The returned FFIString must be freed by the caller using dash_string_free
433+
*/
368434
struct FFIString dash_spv_ffi_config_get_data_dir(const FFIClientConfig *config);
369435

436+
/**
437+
* Destroys an FFIClientConfig and frees its memory
438+
*
439+
* # Safety
440+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet, or null
441+
* - After calling this function, the config pointer becomes invalid and must not be used
442+
* - This function should only be called once per config instance
443+
*/
370444
void dash_spv_ffi_config_destroy(FFIClientConfig *config);
371445

372-
int32_t dash_spv_ffi_config_set_mempool_tracking(FFIClientConfig *config, bool enable);
446+
/**
447+
* Enables or disables mempool tracking
448+
*
449+
* # Safety
450+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
451+
* - The caller must ensure the config pointer remains valid for the duration of this call
452+
*/
453+
int32_t dash_spv_ffi_config_set_mempool_tracking(FFIClientConfig *config,
454+
bool enable);
373455

456+
/**
457+
* Sets the mempool synchronization strategy
458+
*
459+
* # Safety
460+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
461+
* - The caller must ensure the config pointer remains valid for the duration of this call
462+
*/
374463
int32_t dash_spv_ffi_config_set_mempool_strategy(FFIClientConfig *config,
375464
enum FFIMempoolStrategy strategy);
376465

466+
/**
467+
* Sets the maximum number of mempool transactions to track
468+
*
469+
* # Safety
470+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
471+
* - The caller must ensure the config pointer remains valid for the duration of this call
472+
*/
377473
int32_t dash_spv_ffi_config_set_max_mempool_transactions(FFIClientConfig *config,
378474
uint32_t max_transactions);
379475

380-
int32_t dash_spv_ffi_config_set_mempool_timeout(FFIClientConfig *config, uint64_t timeout_secs);
476+
/**
477+
* Sets the mempool transaction timeout in seconds
478+
*
479+
* # Safety
480+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
481+
* - The caller must ensure the config pointer remains valid for the duration of this call
482+
*/
483+
int32_t dash_spv_ffi_config_set_mempool_timeout(FFIClientConfig *config,
484+
uint64_t timeout_secs);
381485

382-
int32_t dash_spv_ffi_config_set_fetch_mempool_transactions(FFIClientConfig *config, bool fetch);
486+
/**
487+
* Sets whether to fetch full mempool transaction data
488+
*
489+
* # Safety
490+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
491+
* - The caller must ensure the config pointer remains valid for the duration of this call
492+
*/
493+
int32_t dash_spv_ffi_config_set_fetch_mempool_transactions(FFIClientConfig *config,
494+
bool fetch);
383495

384-
int32_t dash_spv_ffi_config_set_persist_mempool(FFIClientConfig *config, bool persist);
496+
/**
497+
* Sets whether to persist mempool state to disk
498+
*
499+
* # Safety
500+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
501+
* - The caller must ensure the config pointer remains valid for the duration of this call
502+
*/
503+
int32_t dash_spv_ffi_config_set_persist_mempool(FFIClientConfig *config,
504+
bool persist);
385505

506+
/**
507+
* Gets whether mempool tracking is enabled
508+
*
509+
* # Safety
510+
* - `config` must be a valid pointer to an FFIClientConfig or null
511+
* - If null, returns false as default
512+
*/
386513
bool dash_spv_ffi_config_get_mempool_tracking(const FFIClientConfig *config);
387514

515+
/**
516+
* Gets the mempool synchronization strategy
517+
*
518+
* # Safety
519+
* - `config` must be a valid pointer to an FFIClientConfig or null
520+
* - If null, returns FFIMempoolStrategy::Selective as default
521+
*/
388522
enum FFIMempoolStrategy dash_spv_ffi_config_get_mempool_strategy(const FFIClientConfig *config);
389523

390-
int32_t dash_spv_ffi_config_set_start_from_height(FFIClientConfig *config, uint32_t height);
524+
/**
525+
* Sets the starting block height for synchronization
526+
*
527+
* # Safety
528+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
529+
* - The caller must ensure the config pointer remains valid for the duration of this call
530+
*/
531+
int32_t dash_spv_ffi_config_set_start_from_height(FFIClientConfig *config,
532+
uint32_t height);
391533

392-
int32_t dash_spv_ffi_config_set_wallet_creation_time(FFIClientConfig *config, uint32_t timestamp);
534+
/**
535+
* Sets the wallet creation timestamp for synchronization optimization
536+
*
537+
* # Safety
538+
* - `config` must be a valid pointer to an FFIClientConfig created by dash_spv_ffi_config_new/mainnet/testnet
539+
* - The caller must ensure the config pointer remains valid for the duration of this call
540+
*/
541+
int32_t dash_spv_ffi_config_set_wallet_creation_time(FFIClientConfig *config,
542+
uint32_t timestamp);
393543

394544
const char *dash_spv_ffi_get_last_error(void);
395545

@@ -503,8 +653,6 @@ int32_t dash_spv_ffi_init_logging(const char *level);
503653

504654
const char *dash_spv_ffi_version(void);
505655

506-
const char *dash_spv_ffi_get_network_name(enum FFINetwork network);
507-
508656
void dash_spv_ffi_enable_test_mode(void);
509657

510658
int32_t dash_spv_ffi_client_broadcast_transaction(struct FFIDashSpvClient *client,

dash-spv-ffi/src/broadcast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use crate::{null_check, set_last_error, FFIDashSpvClient, FFIErrorCode};
12
use std::ffi::CStr;
23
use std::os::raw::c_char;
3-
use crate::{null_check, set_last_error, FFIDashSpvClient, FFIErrorCode};
44

55
#[no_mangle]
66
pub unsafe extern "C" fn dash_spv_ffi_client_broadcast_transaction(
@@ -56,4 +56,4 @@ pub unsafe extern "C" fn dash_spv_ffi_client_broadcast_transaction(
5656
FFIErrorCode::from(e) as i32
5757
}
5858
}
59-
}
59+
}

0 commit comments

Comments
 (0)