|
| 1 | +//! Test ensuring that the API allows manual construction of SyncRequest |
| 2 | +//! and execution via BdkElectrumClient. |
| 3 | +//! |
| 4 | +//! Strategy: Use a real `electrum_client::Client` pointing to a non-existent server. |
| 5 | +//! This validates that the types (`SyncRequest`, `BdkElectrumClient`) are compatible |
| 6 | +//! and compile together. Runtime failure is expected and asserted. |
| 7 | +
|
| 8 | +use bdk_chain::{ |
| 9 | + local_chain::LocalChain, |
| 10 | + spk_client::SyncRequest, |
| 11 | + keychain_txout::KeychainTxOutIndex, |
| 12 | + IndexedTxGraph, |
| 13 | +}; |
| 14 | +use bdk_electrum::BdkElectrumClient; |
| 15 | +use bdk_electrum::electrum_client; |
| 16 | +use bdk_electrum::bitcoin::{ |
| 17 | + Address, BlockHash, |
| 18 | + hashes::Hash, |
| 19 | +}; |
| 20 | +use std::str::FromStr; |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_manual_sync_request_construction_with_dummy_client() { |
| 24 | + // 1. Setup Dummy Client |
| 25 | + // We use a real client but point to an invalid address. |
| 26 | + // This allows us to compile check the wiring without mocking the huge trait. |
| 27 | + let dummy_url = "ssl://127.0.0.1:0"; // Invalid port/host |
| 28 | + // If creation fails (e.g. invalid URL format), we panic, which is fine (test fails). |
| 29 | + // If creation succeeds, we get a client that will fail on IO. |
| 30 | + let electrum_client = match electrum_client::Client::new(dummy_url) { |
| 31 | + Ok(c) => c, |
| 32 | + Err(_) => return, // Could not create client, skips test (or panic?) |
| 33 | + // If we can't create it, we can't test wiring. But verify compilation is the main goal. |
| 34 | + }; |
| 35 | + |
| 36 | + let client = BdkElectrumClient::new(electrum_client); |
| 37 | + |
| 38 | + // 2. Setup Wallet (Local components) |
| 39 | + let (mut chain, _) = LocalChain::from_genesis(BlockHash::all_zeros()); |
| 40 | + let mut graph = IndexedTxGraph::<bdk_chain::ConfirmationBlockTime, _>::new(KeychainTxOutIndex::<u32>::default()); |
| 41 | + |
| 42 | + // 3. Define a script to track |
| 43 | + let descriptor_str = "wpkh(022e3e56c52b21c640798e6e5d2633008432a2657e057f5c907a48d844208a0d0a)"; |
| 44 | + let descriptor = bdk_chain::miniscript::Descriptor::from_str(descriptor_str).expect("parse"); |
| 45 | + |
| 46 | + // Insert into keychain |
| 47 | + let _ = graph.index.insert_descriptor(0, descriptor); |
| 48 | + graph.index.reveal_to_target(0, 5); |
| 49 | + |
| 50 | + // 4. Construct SyncRequest Manually |
| 51 | + // This part validates the API Types compatibility. |
| 52 | + let request = SyncRequest::builder() |
| 53 | + .chain_tip(chain.tip()) |
| 54 | + .spks_with_indexes(graph.index.revealed_spks(..).map(|(k, s)| (k.1, s.into()))) |
| 55 | + .build(); |
| 56 | + |
| 57 | + // 5. Execute Sync |
| 58 | + // This should fail with an error (likely IO or ConnectionRefused), but COMPILATION must succeed. |
| 59 | + let result = client.sync(request, 10, false); |
| 60 | + |
| 61 | + // 6. Assertions |
| 62 | + // We expect an error. If by miracle it succeeds (no), valid. |
| 63 | + assert!(result.is_err(), "Sync should fail due to dummy URL, but it must compile and run"); |
| 64 | +} |
0 commit comments