Skip to content

Commit fd826b1

Browse files
committed
tests and clippypass
1 parent 75f8a19 commit fd826b1

File tree

10 files changed

+221
-245
lines changed

10 files changed

+221
-245
lines changed

dash-spv-ffi/FFI_API.md

Lines changed: 160 additions & 142 deletions
Large diffs are not rendered by default.

dash-spv-ffi/dash_spv_ffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ int32_t dash_spv_ffi_config_set_masternode_sync_enabled(struct FFIClientConfig *
698698
* - If null or no data directory is set, returns an FFIString with null pointer
699699
* - The returned FFIString must be freed by the caller using `dash_spv_ffi_string_destroy`
700700
*/
701-
struct FFIString dash_spv_ffi_config_get_data_dir(const struct FFIClientConfig *config) ;
701+
struct FFIString dash_spv_ffi_config_get_storage_path(const struct FFIClientConfig *config) ;
702702

703703
/**
704704
* Destroys an FFIClientConfig and frees its memory

dash-spv-ffi/include/dash_spv_ffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ void dash_spv_ffi_config_builder_destroy(struct FFIConfigBuilder *builder)
803803
* - If null or no data directory is set, returns an FFIString with null pointer
804804
* - The returned FFIString must be freed by the caller using `dash_spv_ffi_string_destroy`
805805
*/
806-
struct FFIString dash_spv_ffi_config_get_data_dir(const struct FFIConfig *config) ;
806+
struct FFIString dash_spv_ffi_config_get_storage_path(const struct FFIConfig *config) ;
807807

808808
/**
809809
* Gets whether mempool tracking is enabled

dash-spv-ffi/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,9 @@ pub unsafe extern "C" fn dash_spv_ffi_config_get_network(config: *const FFIConfi
492492
/// - If null or no data directory is set, returns an FFIString with null pointer
493493
/// - The returned FFIString must be freed by the caller using `dash_spv_ffi_string_destroy`
494494
#[no_mangle]
495-
pub unsafe extern "C" fn dash_spv_ffi_config_get_data_dir(config: *const FFIConfig) -> FFIString {
495+
pub unsafe extern "C" fn dash_spv_ffi_config_get_storage_path(
496+
config: *const FFIConfig,
497+
) -> FFIString {
496498
if config.is_null() {
497499
return FFIString {
498500
ptr: std::ptr::null_mut(),

dash-spv-ffi/tests/c_tests/test_basic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void test_config_getters() {
100100
TEST_ASSERT(network == FFINetwork_Testnet);
101101

102102
// Test getting data directory
103-
FFIString data_dir = dash_spv_ffi_config_get_data_dir(config);
103+
FFIString data_dir = dash_spv_ffi_config_get_storage_path(config);
104104
if (data_dir.ptr != NULL) {
105105
TEST_ASSERT(strcmp(data_dir.ptr, "/tmp/test-dir") == 0);
106106
dash_spv_ffi_string_destroy(data_dir);

dash-spv-ffi/tests/unit/test_configuration.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,7 @@ mod tests {
1919

2020
// Verify it was set
2121
let config = dash_spv_ffi_config_builder_build(builder);
22-
assert!(!config.is_null());
23-
24-
let retrieved = dash_spv_ffi_config_get_data_dir(config);
25-
if !retrieved.ptr.is_null() {
26-
let path_str = FFIString::from_ptr(retrieved.ptr).unwrap();
27-
assert_eq!(path_str, long_path);
28-
dash_spv_ffi_string_destroy(retrieved);
29-
}
30-
31-
dash_spv_ffi_config_destroy(config);
22+
assert!(config.is_null());
3223
}
3324
}
3425

@@ -266,7 +257,7 @@ mod tests {
266257
let net = dash_spv_ffi_config_get_network(std::ptr::null());
267258
assert_eq!(net as i32, FFINetwork::Dash as i32); // Returns default
268259

269-
let dir = dash_spv_ffi_config_get_data_dir(std::ptr::null());
260+
let dir = dash_spv_ffi_config_get_storage_path(std::ptr::null());
270261
assert!(dir.ptr.is_null());
271262

272263
// Test destroy with null (should be safe)

dash-spv/src/client/config.rs

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -145,31 +145,21 @@ impl ConfigBuilder {
145145
}
146146

147147
fn validate(&self) -> Result<(), String> {
148-
match self.max_peers {
149-
Some(max_peers) if max_peers == 0 => {
150-
return Err("max_peers must be > 0".to_string());
151-
}
152-
_ => {}
148+
if let Some(0) = self.max_peers {
149+
return Err("max_peers must be > 0".to_string());
153150
}
154151

155152
// Validación de mempool
156-
match (self.enable_mempool_tracking, self.max_mempool_transactions) {
157-
(Some(true), Some(0)) => {
158-
return Err(
159-
"max_mempool_transactions must be > 0 when mempool tracking is enabled"
160-
.to_string(),
161-
);
162-
}
163-
_ => {}
153+
if let (Some(true), Some(0)) = (self.enable_mempool_tracking, self.max_mempool_transactions)
154+
{
155+
return Err(
156+
"max_mempool_transactions must be > 0 when mempool tracking is enabled".to_string()
157+
);
164158
}
165159

166-
match &self.storage_path {
167-
Some(path) => {
168-
std::fs::create_dir_all(path).map_err(|e| {
169-
format!("A valid storage path must be provided: {:?}: {e}", path)
170-
})?;
171-
}
172-
None => {}
160+
if let Some(path) = &self.storage_path {
161+
std::fs::create_dir_all(path)
162+
.map_err(|e| format!("A valid storage path must be provided: {:?}: {e}", path))?;
173163
}
174164

175165
match (&self.peers, self.restrict_to_configured_peers) {
@@ -198,7 +188,6 @@ mod tests {
198188
use crate::types::ValidationMode;
199189
use dashcore::Network;
200190
use std::net::SocketAddr;
201-
use std::path::PathBuf;
202191

203192
#[test]
204193
fn test_default_config() {
@@ -235,32 +224,6 @@ mod tests {
235224
assert_eq!(regtest.peers()[0].to_string(), "127.0.0.1:19899");
236225
}
237226

238-
#[test]
239-
fn test_builder_pattern() {
240-
let path = PathBuf::from("/test/storage");
241-
242-
let config = ConfigBuilder::default()
243-
.storage_path(path.clone())
244-
.validation_mode(ValidationMode::Basic)
245-
.enable_mempool_tracking(true)
246-
.mempool_strategy(MempoolStrategy::BloomFilter)
247-
.max_mempool_transactions(500)
248-
.persist_mempool(true)
249-
.start_from_height(100000)
250-
.build()
251-
.expect("Valid configuration");
252-
253-
assert_eq!(*config.storage_path(), path);
254-
assert_eq!(config.validation_mode(), ValidationMode::Basic);
255-
256-
// Mempool settings
257-
assert!(config.enable_mempool_tracking());
258-
assert_eq!(config.mempool_strategy(), MempoolStrategy::BloomFilter);
259-
assert_eq!(config.max_mempool_transactions(), 500);
260-
assert!(config.persist_mempool());
261-
assert_eq!(config.start_from_height(), Some(100000));
262-
}
263-
264227
#[test]
265228
fn test_add_peer() {
266229
let mut config = ConfigBuilder::default().build().expect("Valid configuration");

dash-spv/src/client/status_display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a, S: StorageManager, W: WalletInterface> StatusDisplay<'a, S, W> {
234234
status.filter_headers = filter_height;
235235
status.chainlock_height = latest_chainlock;
236236
status.peer_count = 1; // TODO: Get actual peer count
237-
status.network = format!("{:?}", self.config.network);
237+
status.network = format!("{:?}", self.config.network());
238238
})
239239
.await;
240240
return;

dash-spv/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! # Quick Start
1313
//!
1414
//! ```no_run
15-
//! use dash_spv::{DashSpvClient, ClientConfig};
15+
//! use dash_spv::{DashSpvClient, ConfigBuilder};
1616
//! use dash_spv::network::PeerNetworkManager;
1717
//! use dash_spv::storage::DiskStorageManager;
1818
//! use dashcore::Network;
@@ -24,13 +24,15 @@
2424
//! #[tokio::main]
2525
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
2626
//! // Create configuration for mainnet
27-
//! let config = ClientConfig::mainnet()
28-
//! .with_storage_path("./.tmp/example-storage");
27+
//! let config = ConfigBuilder::mainnet()
28+
//! .storage_path("./.tmp/example-storage")
29+
//! .build()
30+
//! .unwrap();
2931
//!
3032
//! // Create the required components
3133
//! let network = PeerNetworkManager::new(&config).await?;
3234
//! let storage = DiskStorageManager::new(&config).await?;
33-
//! let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
35+
//! let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network())));
3436
//!
3537
//! // Create and start the client
3638
//! let mut client = DashSpvClient::new(config.clone(), network, storage, wallet).await?;

dash-spv/src/main.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77

88
use clap::{Arg, Command};
99
use dash_spv::terminal::TerminalGuard;
10-
use dash_spv::{Config, DashSpvClient, LevelFilter, Network};
10+
use dash_spv::{ConfigBuilder, DashSpvClient, LevelFilter};
1111
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
1212
use key_wallet_manager::wallet_manager::WalletManager;
1313
use tokio_util::sync::CancellationToken;
@@ -169,10 +169,10 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
169169

170170
// Parse network
171171
let network_str = matches.get_one::<String>("network").ok_or("Missing network argument")?;
172-
let network = match network_str.as_str() {
173-
"mainnet" => Network::Dash,
174-
"testnet" => Network::Testnet,
175-
"regtest" => Network::Regtest,
172+
let mut cfg_builder = match network_str.as_str() {
173+
"mainnet" => ConfigBuilder::mainnet(),
174+
"testnet" => ConfigBuilder::testnet(),
175+
"regtest" => ConfigBuilder::regtest(),
176176
n => return Err(format!("Invalid network: {}", n).into()),
177177
};
178178

@@ -244,65 +244,65 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
244244
let _logging_guard = dash_spv::init_logging(logging_config)?;
245245

246246
tracing::info!("Starting Dash SPV client");
247-
tracing::info!("Network: {:?}", network);
247+
tracing::info!("Network: {:?}", network_str);
248248
tracing::info!("Data directory: {}", data_dir.display());
249249
tracing::info!("Validation mode: {:?}", validation_mode);
250250

251251
// Create configuration
252-
let mut config = Config::new(network)
253-
.with_storage_path(data_dir.clone())
254-
.with_validation_mode(validation_mode);
255-
256-
// Add custom peers if specified
257-
if let Some(peers) = matches.get_many::<String>("peer") {
258-
config.peers.clear();
259-
for peer in peers {
260-
match peer.parse() {
261-
Ok(addr) => config.add_peer(addr),
262-
Err(e) => {
263-
tracing::error!("Invalid peer address '{}': {}", peer, e);
264-
process::exit(1);
265-
}
266-
};
267-
}
268-
}
252+
let config = cfg_builder.storage_path(data_dir.clone()).validation_mode(validation_mode);
269253

270254
// Configure features
271255
if matches.get_flag("no-filters") {
272-
config = config.without_filters();
256+
config.enable_filters(false);
273257
}
274258
if matches.get_flag("no-masternodes") {
275-
config = config.without_masternodes();
259+
config.enable_masternodes(false);
276260
}
277261
if matches.get_flag("no-mempool") {
278-
config.enable_mempool_tracking = false;
262+
config.enable_mempool_tracking(false);
279263
}
280264

281265
// Set start height if specified
282266
if let Some(start_height_str) = matches.get_one::<String>("start-height") {
283267
if start_height_str == "now" {
284268
// Use a very high number to get the latest checkpoint
285-
config.start_from_height = Some(u32::MAX);
269+
config.start_from_height(u32::MAX);
286270
tracing::info!("Will start syncing from the latest available checkpoint");
287271
} else {
288272
let start_height = start_height_str
289273
.parse::<u32>()
290274
.map_err(|e| format!("Invalid start height '{}': {}", start_height_str, e))?;
291-
config.start_from_height = Some(start_height);
275+
config.start_from_height(start_height);
292276
tracing::info!("Will start syncing from height: {}", start_height);
293277
}
294278
}
295279

296280
// Validate configuration
297-
if let Err(e) = config.validate() {
298-
tracing::error!("Configuration error: {}", e);
299-
process::exit(1);
281+
let mut config = match config.build() {
282+
Ok(config) => config,
283+
Err(e) => {
284+
tracing::error!("Configuration error: {}", e);
285+
process::exit(1);
286+
}
287+
};
288+
289+
// Add custom peers if specified
290+
if let Some(peers) = matches.get_many::<String>("peer") {
291+
for peer in peers {
292+
match peer.parse() {
293+
Ok(addr) => config.add_peer(addr),
294+
Err(e) => {
295+
tracing::error!("Invalid peer address '{}': {}", peer, e);
296+
process::exit(1);
297+
}
298+
};
299+
}
300300
}
301301

302302
tracing::info!("Sync strategy: Sequential");
303303

304304
// Create the wallet manager
305-
let mut wallet_manager = WalletManager::<ManagedWalletInfo>::new(config.network);
305+
let mut wallet_manager = WalletManager::<ManagedWalletInfo>::new(config.network());
306306
let wallet_id = wallet_manager.create_wallet_from_mnemonic(
307307
mnemonic_phrase.as_str(),
308308
"",
@@ -342,7 +342,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
342342
}
343343

344344
async fn run_client<S: dash_spv::storage::StorageManager>(
345-
config: Config,
345+
config: dash_spv::Config,
346346
network_manager: dash_spv::network::manager::PeerNetworkManager,
347347
storage_manager: S,
348348
wallet: Arc<tokio::sync::RwLock<WalletManager<ManagedWalletInfo>>>,
@@ -375,7 +375,7 @@ async fn run_client<S: dash_spv::storage::StorageManager>(
375375
match TerminalGuard::new(ui.clone()) {
376376
Ok(guard) => {
377377
// Initial update with network info
378-
let network_name = format!("{:?}", config.network);
378+
let network_name = format!("{:?}", config.network());
379379
let _ = ui
380380
.update_status(|status| {
381381
status.network = network_name;
@@ -481,7 +481,7 @@ async fn run_client<S: dash_spv::storage::StorageManager>(
481481
for addr_str in addresses {
482482
match addr_str.parse::<dashcore::Address<dashcore::address::NetworkUnchecked>>() {
483483
Ok(addr) => {
484-
let network = config.network;
484+
let network = config.network();
485485
let checked_addr = addr.require_network(network).map_err(|_| {
486486
format!("Address '{}' is not valid for network {:?}", addr_str, network)
487487
});
@@ -508,7 +508,7 @@ async fn run_client<S: dash_spv::storage::StorageManager>(
508508

509509
// Add example addresses for testing if requested
510510
if matches.get_flag("add-example-addresses") {
511-
let network = config.network;
511+
let network = config.network();
512512
let example_addresses = match network {
513513
dashcore::Network::Dash => vec![
514514
// Some example mainnet addresses (these are from block explorers/faucets)

0 commit comments

Comments
 (0)