Skip to content

Commit 3884314

Browse files
authored
refactor: single network WalletManager (#299)
1 parent 1c6ade7 commit 3884314

40 files changed

+247
-634
lines changed

dash-spv-ffi/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub unsafe extern "C" fn dash_spv_ffi_client_new(
183183
let storage = DiskStorageManager::new(storage_path.clone()).await;
184184
let wallet = key_wallet_manager::wallet_manager::WalletManager::<
185185
key_wallet::wallet::managed_wallet_info::ManagedWalletInfo,
186-
>::new();
186+
>::new(client_config.network);
187187
let wallet = std::sync::Arc::new(tokio::sync::RwLock::new(wallet));
188188

189189
match (network, storage) {

dash-spv-ffi/tests/test_wallet_manager.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#[cfg(test)]
22
mod tests {
33
use dash_spv_ffi::*;
4-
use dashcore::Network;
54
use key_wallet::wallet::initialization::WalletAccountCreationOptions;
65
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
76
use key_wallet_ffi::{
87
wallet_manager::{
98
wallet_manager_free_wallet_ids, wallet_manager_get_wallet_ids,
109
wallet_manager_import_wallet_from_bytes, wallet_manager_wallet_count,
1110
},
12-
FFIError, FFINetwork, FFIWalletManager,
11+
FFIError, FFIWalletManager,
1312
};
1413
use key_wallet_manager::wallet_manager::WalletManager;
1514
use std::ffi::CStr;
@@ -58,12 +57,12 @@ mod tests {
5857
let wallet_manager_ptr = wallet_manager as *mut key_wallet_ffi::FFIWalletManager;
5958

6059
// Prepare a serialized wallet using the native manager so we can import it
61-
let mut native_manager = WalletManager::<ManagedWalletInfo>::new();
60+
let mut native_manager =
61+
WalletManager::<ManagedWalletInfo>::new((*config).get_inner().network);
6262
let (serialized_wallet, expected_wallet_id) = native_manager
6363
.create_wallet_from_mnemonic_return_serialized_bytes(
6464
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
6565
"",
66-
Network::Dash,
6766
0,
6867
WalletAccountCreationOptions::Default,
6968
false,
@@ -105,7 +104,6 @@ mod tests {
105104
let mut description_error = FFIError::success();
106105
let description_ptr = key_wallet_ffi::wallet_manager_describe(
107106
wallet_manager_ptr as *const FFIWalletManager,
108-
FFINetwork::Dash,
109107
&mut description_error as *mut FFIError,
110108
);
111109
assert!(!description_ptr.is_null(), "describe should succeed: {:?}", description_error);

dash-spv/examples/filter_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3232
DiskStorageManager::new("./.tmp/filter-sync-example-storage".into()).await?;
3333

3434
// Create wallet manager
35-
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new()));
35+
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
3636

3737
// Create the client
3838
let mut client = DashSpvClient::new(config, network_manager, storage_manager, wallet).await?;

dash-spv/examples/simple_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2828
DiskStorageManager::new("./.tmp/simple-sync-example-storage".into()).await?;
2929

3030
// Create wallet manager
31-
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new()));
31+
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
3232

3333
// Create the client
3434
let mut client = DashSpvClient::new(config, network_manager, storage_manager, wallet).await?;

dash-spv/examples/spv_with_wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3030
DiskStorageManager::new("./.tmp/spv-with-wallet-example-storage".into()).await?;
3131

3232
// Create wallet manager
33-
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new()));
33+
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
3434

3535
// Create the SPV client with all components
3636
let mut client = DashSpvClient::new(config, network_manager, storage_manager, wallet).await?;

dash-spv/src/client/block_processor.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub struct BlockProcessor<W: WalletInterface, S: StorageManager> {
3636
event_tx: mpsc::UnboundedSender<SpvEvent>,
3737
processed_blocks: HashSet<dashcore::BlockHash>,
3838
failed: bool,
39-
network: dashcore::Network,
4039
}
4140

4241
impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync + 'static>
@@ -49,7 +48,6 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
4948
storage: Arc<Mutex<S>>,
5049
stats: Arc<RwLock<SpvStats>>,
5150
event_tx: mpsc::UnboundedSender<SpvEvent>,
52-
network: dashcore::Network,
5351
) -> Self {
5452
Self {
5553
receiver,
@@ -59,7 +57,6 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
5957
event_tx,
6058
processed_blocks: HashSet::new(),
6159
failed: false,
62-
network,
6360
}
6461
}
6562

@@ -177,8 +174,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
177174
} => {
178175
// Check compact filter with wallet
179176
let mut wallet = self.wallet.write().await;
180-
let matches =
181-
wallet.check_compact_filter(&filter, &block_hash, self.network).await;
177+
let matches = wallet.check_compact_filter(&filter, &block_hash).await;
182178

183179
if matches {
184180
tracing::info!("🎯 Compact filter matched for block {}", block_hash);
@@ -191,7 +187,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
191187
tracing::debug!(
192188
"Compact filter did not match for block {}, {}",
193189
block_hash,
194-
wallet.describe(self.network).await
190+
wallet.describe().await
195191
);
196192
drop(wallet);
197193
}
@@ -226,7 +222,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
226222

227223
// Process block with wallet
228224
let mut wallet = self.wallet.write().await;
229-
let txids = wallet.process_block(&block, height, self.network).await;
225+
let txids = wallet.process_block(&block, height).await;
230226
if !txids.is_empty() {
231227
tracing::info!(
232228
"🎯 Wallet found {} relevant transactions in block {} at height {}",
@@ -245,7 +241,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
245241
for txid in &txids {
246242
if let Some(tx) = block.txdata.iter().find(|t| &t.txid() == txid) {
247243
// Ask the wallet for the precise effect of this transaction
248-
let effect = wallet.transaction_effect(tx, self.network).await;
244+
let effect = wallet.transaction_effect(tx).await;
249245
if let Some((net_amount, affected_addresses)) = effect {
250246
tracing::info!("📤 Emitting TransactionDetected event for {}", txid);
251247
let _ = self.event_tx.send(SpvEvent::TransactionDetected {
@@ -291,7 +287,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
291287

292288
// Let the wallet process the mempool transaction
293289
let mut wallet = self.wallet.write().await;
294-
wallet.process_mempool_transaction(&tx, self.network).await;
290+
wallet.process_mempool_transaction(&tx).await;
295291
drop(wallet);
296292

297293
// TODO: Check if transaction affects watched addresses/scripts

dash-spv/src/client/block_processor_test.rs

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,33 @@ mod tests {
4040

4141
#[async_trait::async_trait]
4242
impl key_wallet_manager::wallet_interface::WalletInterface for MockWallet {
43-
async fn process_block(
44-
&mut self,
45-
block: &Block,
46-
height: u32,
47-
_network: Network,
48-
) -> Vec<dashcore::Txid> {
43+
async fn process_block(&mut self, block: &Block, height: u32) -> Vec<dashcore::Txid> {
4944
let mut processed = self.processed_blocks.lock().await;
5045
processed.push((block.block_hash(), height));
5146

5247
// Return txids of all transactions in block as "relevant"
5348
block.txdata.iter().map(|tx| tx.txid()).collect()
5449
}
5550

56-
async fn process_mempool_transaction(&mut self, tx: &Transaction, _network: Network) {
51+
async fn process_mempool_transaction(&mut self, tx: &Transaction) {
5752
let mut processed = self.processed_transactions.lock().await;
5853
processed.push(tx.txid());
5954
}
6055

61-
async fn handle_reorg(&mut self, _from_height: u32, _to_height: u32, _network: Network) {
62-
// Not tested here
63-
}
64-
6556
async fn check_compact_filter(
6657
&mut self,
6758
_filter: &dashcore::bip158::BlockFilter,
6859
_block_hash: &dashcore::BlockHash,
69-
_network: Network,
7060
) -> bool {
7161
// Return true for all filters in test
7262
true
7363
}
7464

75-
async fn describe(&self, _network: Network) -> String {
65+
async fn describe(&self) -> String {
7666
"MockWallet (test implementation)".to_string()
7767
}
7868

79-
async fn transaction_effect(
80-
&self,
81-
tx: &Transaction,
82-
_network: Network,
83-
) -> Option<(i64, Vec<String>)> {
69+
async fn transaction_effect(&self, tx: &Transaction) -> Option<(i64, Vec<String>)> {
8470
let map = self.effects.lock().await;
8571
map.get(&tx.txid()).cloned()
8672
}
@@ -104,14 +90,8 @@ mod tests {
10490
let storage = Arc::new(Mutex::new(
10591
DiskStorageManager::with_temp_dir().await.expect("Failed to create tmp storage"),
10692
));
107-
let processor = BlockProcessor::new(
108-
task_rx,
109-
wallet.clone(),
110-
storage.clone(),
111-
stats,
112-
event_tx,
113-
Network::Dash,
114-
);
93+
let processor =
94+
BlockProcessor::new(task_rx, wallet.clone(), storage.clone(), stats, event_tx);
11595

11696
(processor, task_tx, event_rx, wallet, storage)
11797
}
@@ -268,36 +248,22 @@ mod tests {
268248

269249
#[async_trait::async_trait]
270250
impl key_wallet_manager::wallet_interface::WalletInterface for NonMatchingWallet {
271-
async fn process_block(
272-
&mut self,
273-
_block: &Block,
274-
_height: u32,
275-
_network: Network,
276-
) -> Vec<dashcore::Txid> {
251+
async fn process_block(&mut self, _block: &Block, _height: u32) -> Vec<dashcore::Txid> {
277252
Vec::new()
278253
}
279254

280-
async fn process_mempool_transaction(&mut self, _tx: &Transaction, _network: Network) {}
281-
282-
async fn handle_reorg(
283-
&mut self,
284-
_from_height: u32,
285-
_to_height: u32,
286-
_network: Network,
287-
) {
288-
}
255+
async fn process_mempool_transaction(&mut self, _tx: &Transaction) {}
289256

290257
async fn check_compact_filter(
291258
&mut self,
292259
_filter: &dashcore::bip158::BlockFilter,
293260
_block_hash: &dashcore::BlockHash,
294-
_network: Network,
295261
) -> bool {
296262
// Always return false - filter doesn't match
297263
false
298264
}
299265

300-
async fn describe(&self, _network: Network) -> String {
266+
async fn describe(&self) -> String {
301267
"NonMatchingWallet (test implementation)".to_string()
302268
}
303269
}
@@ -310,8 +276,7 @@ mod tests {
310276
DiskStorageManager::with_temp_dir().await.expect("Failed to create tmp storage"),
311277
));
312278

313-
let processor =
314-
BlockProcessor::new(task_rx, wallet, storage, stats, event_tx, Network::Dash);
279+
let processor = BlockProcessor::new(task_rx, wallet, storage, stats, event_tx);
315280

316281
let block_hash = create_test_block(Network::Dash).block_hash();
317282
let filter_data = vec![1, 2, 3, 4, 5];

dash-spv/src/client/lifecycle.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl<
148148
self.storage.clone(),
149149
self.stats.clone(),
150150
self.event_tx.clone(),
151-
self.config.network,
152151
);
153152

154153
tokio::spawn(async move {

dash-spv/src/client/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod tests {
9797
let network_manager = MockNetworkManager::new();
9898
let storage =
9999
DiskStorageManager::with_temp_dir().await.expect("Failed to create tmp storage");
100-
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new()));
100+
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
101101

102102
let client = DashSpvClient::new(config, network_manager, storage, wallet)
103103
.await
@@ -125,7 +125,7 @@ mod tests {
125125
let network_manager = MockNetworkManager::new();
126126
let storage =
127127
DiskStorageManager::with_temp_dir().await.expect("Failed to create tmp storage");
128-
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new()));
128+
let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
129129

130130
let mut client = DashSpvClient::new(config, network_manager, storage, wallet)
131131
.await

dash-spv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! // Create the required components
3232
//! let network = PeerNetworkManager::new(&config).await?;
3333
//! let storage = DiskStorageManager::new("./.tmp/example-storage".into()).await?;
34-
//! let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new()));
34+
//! let wallet = Arc::new(RwLock::new(WalletManager::<ManagedWalletInfo>::new(config.network)));
3535
//!
3636
//! // Create and start the client
3737
//! let mut client = DashSpvClient::new(config.clone(), network, storage, wallet).await?;

0 commit comments

Comments
 (0)