|
1 |
| -pub mod utils; |
| 1 | +use std::{ |
| 2 | + sync::mpsc::TryRecvError, |
| 3 | + time::{Duration, Instant}, |
| 4 | +}; |
2 | 5 |
|
3 |
| -#[cfg(test)] |
4 |
| -mod tests { |
5 |
| - use std::{ |
6 |
| - sync::mpsc::TryRecvError, |
7 |
| - time::{Duration, Instant}, |
8 |
| - }; |
| 6 | +use anyhow::Result; |
| 7 | +use protocol::{bitcoin::BlockHash, constants::ChainAnchor}; |
| 8 | +use reqwest::blocking::Client; |
| 9 | +use spaced::source::{BitcoinRpc, BitcoinRpcAuth, BlockEvent, BlockFetcher}; |
| 10 | +use testutil::TestRig; |
9 | 11 |
|
10 |
| - use crate::utils::SpaceD; |
11 |
| - use anyhow::Result; |
12 |
| - use bitcoind::bitcoincore_rpc::RpcApi; |
13 |
| - use protocol::constants::ChainAnchor; |
14 |
| - use reqwest::blocking::Client; |
15 |
| - use spaced::source::{BitcoinRpc, BitcoinRpcAuth, BlockEvent, BlockFetcher}; |
16 |
| - use wallet::bitcoin::Network; |
| 12 | +async fn setup(blocks: u64) -> Result<(TestRig, u64, BlockHash)> { |
| 13 | + let rig = TestRig::new().await?; |
| 14 | + rig.mine_blocks(blocks as _, None).await?; |
| 15 | + let height = 0; |
| 16 | + let hash = rig.get_block_hash(height).await?; |
| 17 | + Ok((rig, height, hash)) |
| 18 | +} |
17 | 19 |
|
18 |
| - #[test] |
19 |
| - fn test_block_fetching_from_bitcoin_rpc() -> Result<()> { |
20 |
| - let spaced = SpaceD::new()?; |
21 |
| - let fetcher_rpc = BitcoinRpc::new( |
22 |
| - &spaced.bitcoind.rpc_url(), |
23 |
| - BitcoinRpcAuth::UserPass("user".to_string(), "password".to_string()), |
24 |
| - ); |
25 |
| - let miner_addr = spaced |
26 |
| - .bitcoind |
27 |
| - .client |
28 |
| - .get_new_address(None, None)? |
29 |
| - .require_network(Network::Regtest)?; |
30 |
| - const GENERATED_BLOCKS: u32 = 10; |
31 |
| - spaced |
32 |
| - .bitcoind |
33 |
| - .client |
34 |
| - .generate_to_address(GENERATED_BLOCKS as u64, &miner_addr)?; |
| 20 | +#[test] |
| 21 | +fn test_block_fetching_from_bitcoin_rpc() -> Result<()> { |
| 22 | + const GENERATED_BLOCKS: u64 = 10; |
35 | 23 |
|
36 |
| - let client = Client::new(); |
37 |
| - let (fetcher, receiver) = BlockFetcher::new(fetcher_rpc.clone(), client.clone(), 8); |
38 |
| - fetcher.start(ChainAnchor { |
39 |
| - hash: fetcher_rpc.send_json_blocking(&client, &fetcher_rpc.get_block_hash(0))?, |
40 |
| - height: 0, |
41 |
| - }); |
| 24 | + let (rig, mut height, hash) = tokio::runtime::Runtime::new()? |
| 25 | + .block_on(setup(GENERATED_BLOCKS))?; |
| 26 | + let fetcher_rpc = BitcoinRpc::new( |
| 27 | + &rig.bitcoind.rpc_url(), |
| 28 | + BitcoinRpcAuth::UserPass("user".to_string(), "password".to_string()), |
| 29 | + ); |
42 | 30 |
|
43 |
| - let mut start_block = 0; |
44 |
| - let timeout = Duration::from_secs(5); |
45 |
| - let start_time = Instant::now(); |
| 31 | + let client = Client::new(); |
| 32 | + let (fetcher, receiver) = BlockFetcher::new(fetcher_rpc.clone(), client.clone(), 8); |
46 | 33 |
|
47 |
| - loop { |
48 |
| - if start_time.elapsed() > timeout { |
49 |
| - panic!("Test timed out after {:?}", timeout); |
50 |
| - } |
51 |
| - match receiver.try_recv() { |
52 |
| - Ok(BlockEvent::Block(id, _)) => { |
53 |
| - start_block += 1; |
54 |
| - if id.height == GENERATED_BLOCKS { |
55 |
| - break; |
56 |
| - } |
57 |
| - } |
58 |
| - Ok(BlockEvent::Error(e)) => panic!("Unexpected error: {}", e), |
59 |
| - Err(TryRecvError::Empty) => { |
60 |
| - std::thread::sleep(Duration::from_millis(10)); |
| 34 | + fetcher.start(ChainAnchor { hash, height: 0 }); |
| 35 | + |
| 36 | + let timeout = Duration::from_secs(5); |
| 37 | + let start_time = Instant::now(); |
| 38 | + |
| 39 | + loop { |
| 40 | + if start_time.elapsed() > timeout { |
| 41 | + panic!("Test timed out after {:?}", timeout); |
| 42 | + } |
| 43 | + match receiver.try_recv() { |
| 44 | + Ok(BlockEvent::Block(id, _)) => { |
| 45 | + height += 1; |
| 46 | + if id.height == GENERATED_BLOCKS as u32 { |
| 47 | + break; |
61 | 48 | }
|
62 |
| - Err(TryRecvError::Disconnected) => panic!("Disconnected unexpectedly"), |
63 | 49 | }
|
| 50 | + Ok(BlockEvent::Error(e)) => panic!("Unexpected error: {}", e), |
| 51 | + Err(TryRecvError::Empty) => { |
| 52 | + std::thread::sleep(Duration::from_millis(10)); |
| 53 | + } |
| 54 | + Err(TryRecvError::Disconnected) => panic!("Disconnected unexpectedly"), |
64 | 55 | }
|
65 |
| - assert_eq!( |
66 |
| - start_block, GENERATED_BLOCKS, |
67 |
| - "Not all blocks were received" |
68 |
| - ); |
69 |
| - Ok(()) |
70 | 56 | }
|
| 57 | + |
| 58 | + assert_eq!(height, GENERATED_BLOCKS, "Not all blocks were received"); |
| 59 | + Ok(()) |
71 | 60 | }
|
0 commit comments