Skip to content

Commit dcbe06a

Browse files
committed
fixed clippt warnings by taking the spv client
1 parent b43a6e8 commit dcbe06a

1 file changed

Lines changed: 73 additions & 40 deletions

File tree

dash-spv-ffi/src/client.rs

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,18 +1687,27 @@ pub unsafe extern "C" fn dash_spv_ffi_client_get_filter_matched_heights(
16871687
let inner = client.inner.clone();
16881688

16891689
let result = client.runtime.block_on(async {
1690-
// Get chain state without taking the client
1691-
let chain_state = {
1692-
let guard = inner.lock().unwrap();
1693-
match guard.as_ref() {
1694-
Some(client) => client.chain_state().await,
1690+
// Take client out of the mutex so no std::sync::MutexGuard is held across .await
1691+
let spv_client = {
1692+
let mut guard = inner.lock().unwrap();
1693+
match guard.take() {
1694+
Some(client) => client,
16951695
None => {
16961696
set_last_error("Client not initialized");
16971697
return None;
16981698
}
16991699
}
17001700
};
17011701

1702+
// Query chain state without holding the mutex
1703+
let chain_state = spv_client.chain_state().await;
1704+
1705+
// Put client back
1706+
{
1707+
let mut guard = inner.lock().unwrap();
1708+
*guard = Some(spv_client);
1709+
}
1710+
17021711
// Get filter matches in range - works even during sync
17031712
let matches_result = chain_state.get_filter_matched_heights(start_height..end_height);
17041713

@@ -1765,21 +1774,33 @@ pub unsafe extern "C" fn dash_spv_ffi_client_get_transaction_count(
17651774
let inner = client.inner.clone();
17661775

17671776
let result = client.runtime.block_on(async {
1768-
// Get wallet without taking the client
1769-
let guard = inner.lock().unwrap();
1770-
match guard.as_ref() {
1771-
Some(spv_client) => {
1772-
// Access wallet and get transaction count
1773-
let wallet = spv_client.wallet();
1774-
let wallet_guard = wallet.read().await;
1775-
let tx_history = wallet_guard.transaction_history();
1776-
tx_history.len()
1777-
}
1778-
None => {
1779-
tracing::warn!("Client not initialized when querying transaction count");
1780-
0
1777+
// Take client out of the mutex so no std::sync::MutexGuard is held across .await
1778+
let spv_client = {
1779+
let mut guard = inner.lock().unwrap();
1780+
match guard.take() {
1781+
Some(client) => client,
1782+
None => {
1783+
tracing::warn!("Client not initialized when querying transaction count");
1784+
return 0;
1785+
}
17811786
}
1787+
};
1788+
1789+
// Access wallet and get transaction count
1790+
let tx_len = {
1791+
let wallet = spv_client.wallet();
1792+
let wallet_guard = wallet.read().await;
1793+
let tx_history = wallet_guard.transaction_history();
1794+
tx_history.len()
1795+
};
1796+
1797+
// Put client back
1798+
{
1799+
let mut guard = inner.lock().unwrap();
1800+
*guard = Some(spv_client);
17821801
}
1802+
1803+
tx_len
17831804
});
17841805

17851806
result
@@ -1810,32 +1831,44 @@ pub unsafe extern "C" fn dash_spv_ffi_client_get_blocks_with_transactions_count(
18101831
let inner = client.inner.clone();
18111832

18121833
let result = client.runtime.block_on(async {
1813-
// Get wallet without taking the client
1814-
let guard = inner.lock().unwrap();
1815-
match guard.as_ref() {
1816-
Some(spv_client) => {
1817-
// Access wallet and get unique block heights
1818-
let wallet = spv_client.wallet();
1819-
let wallet_guard = wallet.read().await;
1820-
let tx_history = wallet_guard.transaction_history();
1821-
1822-
// Count unique block heights (confirmed transactions only)
1823-
let mut unique_heights = std::collections::HashSet::new();
1824-
for tx in tx_history {
1825-
if let Some(height) = tx.height {
1826-
unique_heights.insert(height);
1827-
}
1834+
// Take client out of the mutex so no std::sync::MutexGuard is held across .await
1835+
let spv_client = {
1836+
let mut guard = inner.lock().unwrap();
1837+
match guard.take() {
1838+
Some(client) => client,
1839+
None => {
1840+
tracing::warn!(
1841+
"Client not initialized when querying blocks with transactions count"
1842+
);
1843+
return 0;
18281844
}
1829-
1830-
unique_heights.len()
18311845
}
1832-
None => {
1833-
tracing::warn!(
1834-
"Client not initialized when querying blocks with transactions count"
1835-
);
1836-
0
1846+
};
1847+
1848+
let unique_heights_len = {
1849+
// Access wallet and get unique block heights
1850+
let wallet = spv_client.wallet();
1851+
let wallet_guard = wallet.read().await;
1852+
let tx_history = wallet_guard.transaction_history();
1853+
1854+
// Count unique block heights (confirmed transactions only)
1855+
let mut unique_heights = std::collections::HashSet::new();
1856+
for tx in tx_history {
1857+
if let Some(height) = tx.height {
1858+
unique_heights.insert(height);
1859+
}
18371860
}
1861+
1862+
unique_heights.len()
1863+
};
1864+
1865+
// Put client back
1866+
{
1867+
let mut guard = inner.lock().unwrap();
1868+
*guard = Some(spv_client);
18381869
}
1870+
1871+
unique_heights_len
18391872
});
18401873

18411874
result

0 commit comments

Comments
 (0)