Skip to content

Commit f15ac46

Browse files
wallet manager integration into dash-spv
1 parent 8a8c697 commit f15ac46

31 files changed

+838
-3406
lines changed

dash-spv/examples/filter_sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use dash_spv::network::MultiPeerNetworkManager;
44
use dash_spv::storage::MemoryStorageManager;
5-
use dash_spv::{init_logging, ClientConfig, DashSpvClient, WatchItem};
5+
use dash_spv::{init_logging, ClientConfig, DashSpvClient};
66
use dashcore::{Address, Network};
77
use key_wallet_manager::spv_wallet_manager::SPVWalletManager;
88
use std::str::FromStr;
@@ -31,7 +31,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3131
let storage_manager = MemoryStorageManager::new().await?;
3232

3333
// Create wallet manager
34-
let wallet = Arc::new(RwLock::new(SPVWalletManager::new(config.network)));
34+
let wallet = Arc::new(RwLock::new(SPVWalletManager::new()));
3535

3636
// Create the client
3737
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
@@ -24,7 +24,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2424
let storage_manager = MemoryStorageManager::new().await?;
2525

2626
// Create wallet manager
27-
let wallet = Arc::new(RwLock::new(SPVWalletManager::new(config.network)));
27+
let wallet = Arc::new(RwLock::new(SPVWalletManager::new()));
2828

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

dash-spv/src/client/block_processor.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub enum BlockProcessingTask {
2121
response_tx: oneshot::Sender<Result<()>>,
2222
},
2323
ProcessCompactFilter {
24-
filter_data: Vec<u8>,
24+
filter: dashcore::bip158::BlockFilter,
2525
block_hash: dashcore::BlockHash,
2626
response_tx: oneshot::Sender<Result<bool>>,
2727
},
@@ -37,6 +37,7 @@ pub struct BlockProcessor<W: WalletInterface, S: StorageManager> {
3737
event_tx: mpsc::UnboundedSender<SpvEvent>,
3838
processed_blocks: HashSet<dashcore::BlockHash>,
3939
failed: bool,
40+
network: dashcore::Network,
4041
}
4142

4243
impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync + 'static>
@@ -50,6 +51,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
5051
watch_items: Arc<RwLock<HashSet<WatchItem>>>,
5152
stats: Arc<RwLock<SpvStats>>,
5253
event_tx: mpsc::UnboundedSender<SpvEvent>,
54+
network: dashcore::Network,
5355
) -> Self {
5456
Self {
5557
receiver,
@@ -60,6 +62,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
6062
event_tx,
6163
processed_blocks: HashSet::new(),
6264
failed: false,
65+
network,
6366
}
6467
}
6568

@@ -171,13 +174,14 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
171174
let _ = response_tx.send(result);
172175
}
173176
BlockProcessingTask::ProcessCompactFilter {
174-
filter_data,
177+
filter,
175178
block_hash,
176179
response_tx,
177180
} => {
178181
// Check compact filter with wallet
179182
let wallet = self.wallet.read().await;
180-
let matches = wallet.check_compact_filter(&filter_data, &block_hash).await;
183+
let matches =
184+
wallet.check_compact_filter(&filter, &block_hash, self.network).await;
181185
drop(wallet);
182186

183187
if matches {
@@ -221,7 +225,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
221225

222226
// Process block with wallet
223227
let mut wallet = self.wallet.write().await;
224-
let txids = wallet.process_block(&block, height).await;
228+
let txids = wallet.process_block(&block, height, self.network).await;
225229
if !txids.is_empty() {
226230
tracing::info!(
227231
"🎯 Wallet found {} relevant transactions in block {} at height {}",
@@ -259,7 +263,7 @@ impl<W: WalletInterface + Send + Sync + 'static, S: StorageManager + Send + Sync
259263

260264
// Let the wallet process the mempool transaction
261265
let mut wallet = self.wallet.write().await;
262-
wallet.process_mempool_transaction(&tx).await;
266+
wallet.process_mempool_transaction(&tx, self.network).await;
263267
drop(wallet);
264268

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

dash-spv/src/client/block_processor_test.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,33 @@ mod tests {
3535

3636
#[async_trait::async_trait]
3737
impl key_wallet_manager::wallet_interface::WalletInterface for MockWallet {
38-
async fn process_block(&mut self, block: &Block, height: u32) -> Vec<dashcore::Txid> {
38+
async fn process_block(
39+
&mut self,
40+
block: &Block,
41+
height: u32,
42+
_network: Network,
43+
) -> Vec<dashcore::Txid> {
3944
let mut processed = self.processed_blocks.lock().await;
4045
processed.push((block.block_hash(), height));
4146

4247
// Return txids of all transactions in block as "relevant"
4348
block.txdata.iter().map(|tx| tx.txid()).collect()
4449
}
4550

46-
async fn process_mempool_transaction(&mut self, tx: &Transaction) {
51+
async fn process_mempool_transaction(&mut self, tx: &Transaction, _network: Network) {
4752
let mut processed = self.processed_transactions.lock().await;
4853
processed.push(tx.txid());
4954
}
5055

51-
async fn handle_reorg(&mut self, _from_height: u32, _to_height: u32) {
56+
async fn handle_reorg(&mut self, _from_height: u32, _to_height: u32, _network: Network) {
5257
// Not tested here
5358
}
5459

5560
async fn check_compact_filter(
5661
&self,
57-
_filter: &[u8],
62+
_filter: &dashcore::bip158::BlockFilter,
5863
_block_hash: &dashcore::BlockHash,
64+
_network: Network,
5965
) -> bool {
6066
// Return true for all filters in test
6167
true
@@ -91,6 +97,7 @@ mod tests {
9197
watch_items,
9298
stats,
9399
event_tx,
100+
Network::Dash,
94101
);
95102

96103
(processor, task_tx, event_rx, wallet, storage)
@@ -169,9 +176,10 @@ mod tests {
169176

170177
// Send filter processing task
171178
let (response_tx, response_rx) = oneshot::channel();
179+
let filter = dashcore::bip158::BlockFilter::new(&filter_data);
172180
task_tx
173181
.send(BlockProcessingTask::ProcessCompactFilter {
174-
filter_data: filter_data.clone(),
182+
filter,
175183
block_hash,
176184
response_tx,
177185
})
@@ -219,18 +227,30 @@ mod tests {
219227

220228
#[async_trait::async_trait]
221229
impl key_wallet_manager::wallet_interface::WalletInterface for NonMatchingWallet {
222-
async fn process_block(&mut self, _block: &Block, _height: u32) -> Vec<dashcore::Txid> {
230+
async fn process_block(
231+
&mut self,
232+
_block: &Block,
233+
_height: u32,
234+
_network: Network,
235+
) -> Vec<dashcore::Txid> {
223236
Vec::new()
224237
}
225238

226-
async fn process_mempool_transaction(&mut self, _tx: &Transaction) {}
239+
async fn process_mempool_transaction(&mut self, _tx: &Transaction, _network: Network) {}
227240

228-
async fn handle_reorg(&mut self, _from_height: u32, _to_height: u32) {}
241+
async fn handle_reorg(
242+
&mut self,
243+
_from_height: u32,
244+
_to_height: u32,
245+
_network: Network,
246+
) {
247+
}
229248

230249
async fn check_compact_filter(
231250
&self,
232-
_filter: &[u8],
251+
_filter: &dashcore::bip158::BlockFilter,
233252
_block_hash: &dashcore::BlockHash,
253+
_network: Network,
234254
) -> bool {
235255
// Always return false - filter doesn't match
236256
false
@@ -250,17 +270,25 @@ mod tests {
250270
let storage = Arc::new(Mutex::new(MemoryStorageManager::new().await.unwrap()));
251271
let watch_items = Arc::new(RwLock::new(HashSet::new()));
252272

253-
let mut processor =
254-
BlockProcessor::new(task_rx, wallet, storage, watch_items, stats, event_tx);
273+
let mut processor = BlockProcessor::new(
274+
task_rx,
275+
wallet,
276+
storage,
277+
watch_items,
278+
stats,
279+
event_tx,
280+
Network::Dash,
281+
);
255282

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

259286
// Send filter processing task
260287
let (response_tx, response_rx) = oneshot::channel();
288+
let filter = dashcore::bip158::BlockFilter::new(&filter_data);
261289
task_tx
262290
.send(BlockProcessingTask::ProcessCompactFilter {
263-
filter_data,
291+
filter,
264292
block_hash,
265293
response_tx,
266294
})

dash-spv/src/client/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ impl<
359359
self.watch_items.clone(),
360360
self.stats.clone(),
361361
self.event_tx.clone(),
362+
self.config.network,
362363
);
363364

364365
tokio::spawn(async move {

dash-spv/src/client/watch_manager_test.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,33 @@ mod tests {
3434
&mut self,
3535
_block: &dashcore::Block,
3636
_height: u32,
37+
_network: dashcore::Network,
3738
) -> Vec<dashcore::Txid> {
3839
Vec::new()
3940
}
4041

41-
async fn process_mempool_transaction(&mut self, _tx: &dashcore::Transaction) {
42+
async fn process_mempool_transaction(
43+
&mut self,
44+
_tx: &dashcore::Transaction,
45+
_network: dashcore::Network,
46+
) {
4247
// Not used in these tests
4348
}
4449

45-
async fn handle_reorg(&mut self, _from_height: u32, _to_height: u32) {
50+
async fn handle_reorg(
51+
&mut self,
52+
_from_height: u32,
53+
_to_height: u32,
54+
_network: dashcore::Network,
55+
) {
4656
// Not used in these tests
4757
}
4858

4959
async fn check_compact_filter(
5060
&self,
51-
_filter: &[u8],
61+
_filter: &dashcore::bip158::BlockFilter,
5262
_block_hash: &dashcore::BlockHash,
63+
_network: dashcore::Network,
5364
) -> bool {
5465
false
5566
}

dash-spv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! // Create the required components
3131
//! let network = MultiPeerNetworkManager::new(&config).await?;
3232
//! let storage = MemoryStorageManager::new().await?;
33-
//! let wallet = Arc::new(RwLock::new(SPVWalletManager::new(config.network)));
33+
//! let wallet = Arc::new(RwLock::new(SPVWalletManager::new()));
3434
//!
3535
//! // Create and start the client
3636
//! let mut client = DashSpvClient::new(config.clone(), network, storage, wallet).await?;

dash-spv/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
221221
tracing::info!("Using data directory: {}", data_dir.display());
222222

223223
// Create the SPV wallet manager
224-
let spv_wallet = key_wallet_manager::spv_wallet_manager::SPVWalletManager::new(network);
224+
let spv_wallet = key_wallet_manager::spv_wallet_manager::SPVWalletManager::new();
225225
let wallet = Arc::new(tokio::sync::RwLock::new(spv_wallet));
226226

227227
// Create network manager

dash-spv/tests/chainlock_simple_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async fn test_chainlock_validation_flow() {
5050
DiskStorageManager::new(config.storage_path.clone().unwrap()).await.unwrap();
5151

5252
// Create wallet manager
53-
let wallet = Arc::new(RwLock::new(SPVWalletManager::new(config.network)));
53+
let wallet = Arc::new(RwLock::new(SPVWalletManager::new()));
5454

5555
// Create the SPV client
5656
let mut client =
@@ -100,7 +100,7 @@ async fn test_chainlock_manager_initialization() {
100100
DiskStorageManager::new(config.storage_path.clone().unwrap()).await.unwrap();
101101

102102
// Create wallet manager
103-
let wallet = Arc::new(RwLock::new(SPVWalletManager::new(config.network)));
103+
let wallet = Arc::new(RwLock::new(SPVWalletManager::new()));
104104

105105
// Create the SPV client
106106
let client =

dash-spv/tests/header_sync_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ async fn test_header_sync_with_client_integration() {
306306
MemoryStorageManager::new().await.expect("Failed to create storage manager");
307307

308308
// Create wallet manager
309-
let wallet = Arc::new(RwLock::new(SPVWalletManager::new(config.network)));
309+
let wallet = Arc::new(RwLock::new(SPVWalletManager::new()));
310310

311311
let client = DashSpvClient::new(config, network_manager, storage_manager, wallet).await;
312312
assert!(client.is_ok(), "Client creation should succeed");

0 commit comments

Comments
 (0)