1 #![cfg(all(not(target_os = "windows"), any(feature = "esplora-blocking", feature = "esplora-async", feature = "electrum")))]
3 #[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
4 use lightning_transaction_sync::EsploraSyncClient;
5 #[cfg(feature = "electrum")]
6 use lightning_transaction_sync::ElectrumSyncClient;
7 use lightning::chain::{Confirm, Filter, WatchedOutput};
8 use lightning::chain::transaction::{OutPoint, TransactionData};
9 use lightning::util::test_utils::TestLogger;
11 use electrsd::{bitcoind, bitcoind::BitcoinD, ElectrsD};
12 use bitcoin::{Amount, Txid, BlockHash};
13 use bitcoin::blockdata::block::Header;
14 use bitcoin::blockdata::constants::genesis_block;
15 use bitcoin::network::constants::Network;
16 use electrsd::bitcoind::bitcoincore_rpc::bitcoincore_rpc_json::AddressType;
17 use bitcoind::bitcoincore_rpc::RpcApi;
18 use bdk_macros::maybe_await;
22 use std::time::Duration;
23 use std::collections::{HashMap, HashSet};
25 pub fn setup_bitcoind_and_electrsd() -> (BitcoinD, ElectrsD) {
27 env::var("BITCOIND_EXE").ok().or_else(|| bitcoind::downloaded_exe_path().ok()).expect(
28 "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature",
30 let mut bitcoind_conf = bitcoind::Conf::default();
31 bitcoind_conf.network = "regtest";
32 let bitcoind = BitcoinD::with_conf(bitcoind_exe, &bitcoind_conf).unwrap();
34 let electrs_exe = env::var("ELECTRS_EXE")
36 .or_else(electrsd::downloaded_exe_path)
37 .expect("you need to provide env var ELECTRS_EXE or specify an electrsd version feature");
38 let mut electrsd_conf = electrsd::Conf::default();
39 electrsd_conf.http_enabled = true;
40 electrsd_conf.network = "regtest";
41 let electrsd = ElectrsD::with_conf(electrs_exe, &bitcoind, &electrsd_conf).unwrap();
45 pub fn generate_blocks_and_wait(bitcoind: &BitcoinD, electrsd: &ElectrsD, num: usize) {
46 let cur_height = bitcoind.client.get_block_count().expect("failed to get current block height");
47 let address = bitcoind
49 .get_new_address(Some("test"), Some(AddressType::Legacy))
50 .expect("failed to get new address")
52 // TODO: expect this Result once the WouldBlock issue is resolved upstream.
53 let _block_hashes_res = bitcoind.client.generate_to_address(num as u64, &address);
54 wait_for_block(electrsd, cur_height as usize + num);
57 pub fn wait_for_block(electrsd: &ElectrsD, min_height: usize) {
58 use electrsd::electrum_client::ElectrumApi;
59 let mut header = match electrsd.client.block_headers_subscribe_raw() {
62 // While subscribing should succeed the first time around, we ran into some cases where
63 // it didn't. Since we can't proceed without subscribing, we try again after a delay
64 // and panic if it still fails.
65 std::thread::sleep(Duration::from_secs(1));
66 electrsd.client.block_headers_subscribe_raw().expect("failed to subscribe to block headers")
70 if header.height >= min_height {
73 header = exponential_backoff_poll(|| {
74 electrsd.trigger().expect("failed to trigger electrsd");
75 electrsd.client.ping().expect("failed to ping electrsd");
76 electrsd.client.block_headers_pop_raw().expect("failed to pop block header")
81 fn exponential_backoff_poll<T, F>(mut poll: F) -> T
83 F: FnMut() -> Option<T>,
85 let mut delay = Duration::from_millis(64);
89 Some(data) => break data,
90 None if delay.as_millis() < 512 => {
91 delay = delay.mul_f32(2.0);
94 None if tries == 10 => panic!("Exceeded our maximum wait time."),
98 std::thread::sleep(delay);
103 enum TestConfirmableEvent {
104 Confirmed(Txid, BlockHash, u32),
106 BestBlockUpdated(BlockHash, u32),
109 struct TestConfirmable {
110 pub confirmed_txs: Mutex<HashMap<Txid, (BlockHash, u32)>>,
111 pub unconfirmed_txs: Mutex<HashSet<Txid>>,
112 pub best_block: Mutex<(BlockHash, u32)>,
113 pub events: Mutex<Vec<TestConfirmableEvent>>,
116 impl TestConfirmable {
117 pub fn new() -> Self {
118 let genesis_hash = genesis_block(Network::Regtest).block_hash();
120 confirmed_txs: Mutex::new(HashMap::new()),
121 unconfirmed_txs: Mutex::new(HashSet::new()),
122 best_block: Mutex::new((genesis_hash, 0)),
123 events: Mutex::new(Vec::new()),
128 impl Confirm for TestConfirmable {
129 fn transactions_confirmed(&self, header: &Header, txdata: &TransactionData<'_>, height: u32) {
130 for (_, tx) in txdata {
131 let txid = tx.txid();
132 let block_hash = header.block_hash();
133 self.confirmed_txs.lock().unwrap().insert(txid, (block_hash, height));
134 self.unconfirmed_txs.lock().unwrap().remove(&txid);
135 self.events.lock().unwrap().push(TestConfirmableEvent::Confirmed(txid, block_hash, height));
139 fn transaction_unconfirmed(&self, txid: &Txid) {
140 self.unconfirmed_txs.lock().unwrap().insert(*txid);
141 self.confirmed_txs.lock().unwrap().remove(txid);
142 self.events.lock().unwrap().push(TestConfirmableEvent::Unconfirmed(*txid));
145 fn best_block_updated(&self, header: &Header, height: u32) {
146 let block_hash = header.block_hash();
147 *self.best_block.lock().unwrap() = (block_hash, height);
148 self.events.lock().unwrap().push(TestConfirmableEvent::BestBlockUpdated(block_hash, height));
151 fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {
152 self.confirmed_txs.lock().unwrap().iter().map(|(&txid, (hash, height))| (txid, *height, Some(*hash))).collect::<Vec<_>>()
156 macro_rules! test_syncing {
157 ($tx_sync: expr, $confirmable: expr, $bitcoind: expr, $electrsd: expr) => {{
158 // Check we pick up on new best blocks
159 assert_eq!($confirmable.best_block.lock().unwrap().1, 0);
161 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
162 assert_eq!($confirmable.best_block.lock().unwrap().1, 102);
164 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
165 assert_eq!(events.len(), 1);
167 // Check registered confirmed transactions are marked confirmed
168 let new_address = $bitcoind.client.get_new_address(Some("test"),
169 Some(AddressType::Legacy)).unwrap().assume_checked();
170 let txid = $bitcoind.client.send_to_address(&new_address, Amount::from_sat(5000), None, None,
171 None, None, None, None).unwrap();
172 let second_txid = $bitcoind.client.send_to_address(&new_address, Amount::from_sat(5000), None,
173 None, None, None, None, None).unwrap();
174 $tx_sync.register_tx(&txid, &new_address.payload.script_pubkey());
176 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
178 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
179 assert_eq!(events.len(), 0);
180 assert!($confirmable.confirmed_txs.lock().unwrap().is_empty());
181 assert!($confirmable.unconfirmed_txs.lock().unwrap().is_empty());
183 generate_blocks_and_wait(&$bitcoind, &$electrsd, 1);
184 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
186 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
187 assert_eq!(events.len(), 2);
188 assert!($confirmable.confirmed_txs.lock().unwrap().contains_key(&txid));
189 assert!($confirmable.unconfirmed_txs.lock().unwrap().is_empty());
191 // Now take an arbitrary output of the second transaction and check we'll confirm its spend.
192 let tx_res = $bitcoind.client.get_transaction(&second_txid, None).unwrap();
193 let block_hash = tx_res.info.blockhash.unwrap();
194 let tx = tx_res.transaction().unwrap();
195 let prev_outpoint = tx.input.first().unwrap().previous_output;
196 let prev_tx = $bitcoind.client.get_transaction(&prev_outpoint.txid, None).unwrap().transaction()
198 let prev_script_pubkey = prev_tx.output[prev_outpoint.vout as usize].script_pubkey.clone();
199 let output = WatchedOutput {
200 block_hash: Some(block_hash),
201 outpoint: OutPoint { txid: prev_outpoint.txid, index: prev_outpoint.vout as u16 },
202 script_pubkey: prev_script_pubkey
205 $tx_sync.register_output(output);
206 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
208 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
209 assert_eq!(events.len(), 1);
210 assert!($confirmable.confirmed_txs.lock().unwrap().contains_key(&second_txid));
211 assert_eq!($confirmable.confirmed_txs.lock().unwrap().len(), 2);
212 assert!($confirmable.unconfirmed_txs.lock().unwrap().is_empty());
214 // Check previously confirmed transactions are marked unconfirmed when they are reorged.
215 let best_block_hash = $bitcoind.client.get_best_block_hash().unwrap();
216 $bitcoind.client.invalidate_block(&best_block_hash).unwrap();
218 // We're getting back to the previous height with a new tip, but best block shouldn't change.
219 generate_blocks_and_wait(&$bitcoind, &$electrsd, 1);
220 assert_ne!($bitcoind.client.get_best_block_hash().unwrap(), best_block_hash);
221 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
222 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
223 assert_eq!(events.len(), 0);
225 // Now we're surpassing previous height, getting new tip.
226 generate_blocks_and_wait(&$bitcoind, &$electrsd, 1);
227 assert_ne!($bitcoind.client.get_best_block_hash().unwrap(), best_block_hash);
228 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
230 // Transactions still confirmed but under new tip.
231 assert!($confirmable.confirmed_txs.lock().unwrap().contains_key(&txid));
232 assert!($confirmable.confirmed_txs.lock().unwrap().contains_key(&second_txid));
233 assert!($confirmable.unconfirmed_txs.lock().unwrap().is_empty());
235 // Check we got unconfirmed, then reconfirmed in the meantime.
236 let mut seen_txids = HashSet::new();
237 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
238 assert_eq!(events.len(), 5);
241 TestConfirmableEvent::Unconfirmed(t) => {
242 assert!(t == txid || t == second_txid);
243 assert!(seen_txids.insert(t));
245 _ => panic!("Unexpected event"),
249 TestConfirmableEvent::Unconfirmed(t) => {
250 assert!(t == txid || t == second_txid);
251 assert!(seen_txids.insert(t));
253 _ => panic!("Unexpected event"),
257 TestConfirmableEvent::BestBlockUpdated(..) => {},
258 _ => panic!("Unexpected event"),
262 TestConfirmableEvent::Confirmed(t, _, _) => {
263 assert!(t == txid || t == second_txid);
264 assert!(seen_txids.remove(&t));
266 _ => panic!("Unexpected event"),
270 TestConfirmableEvent::Confirmed(t, _, _) => {
271 assert!(t == txid || t == second_txid);
272 assert!(seen_txids.remove(&t));
274 _ => panic!("Unexpected event"),
277 assert_eq!(seen_txids.len(), 0);
282 #[cfg(feature = "esplora-blocking")]
283 fn test_esplora_syncs() {
284 let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
285 generate_blocks_and_wait(&bitcoind, &electrsd, 101);
286 let mut logger = TestLogger::new();
287 let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
288 let tx_sync = EsploraSyncClient::new(esplora_url, &mut logger);
289 let confirmable = TestConfirmable::new();
291 test_syncing!(tx_sync, confirmable, bitcoind, electrsd);
295 #[cfg(feature = "esplora-async")]
296 async fn test_esplora_syncs() {
297 let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
298 generate_blocks_and_wait(&bitcoind, &electrsd, 101);
299 let mut logger = TestLogger::new();
300 let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
301 let tx_sync = EsploraSyncClient::new(esplora_url, &mut logger);
302 let confirmable = TestConfirmable::new();
304 test_syncing!(tx_sync, confirmable, bitcoind, electrsd);
308 #[cfg(feature = "electrum")]
309 fn test_electrum_syncs() {
310 let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
311 generate_blocks_and_wait(&bitcoind, &electrsd, 101);
312 let mut logger = TestLogger::new();
313 let electrum_url = format!("tcp://{}", electrsd.electrum_url);
314 let tx_sync = ElectrumSyncClient::new(electrum_url, &mut logger).unwrap();
315 let confirmable = TestConfirmable::new();
316 test_syncing!(tx_sync, confirmable, bitcoind, electrsd);