DRY up Esplora/Electrum `integration_tests`
[rust-lightning] / lightning-transaction-sync / tests / integration_tests.rs
1 #![cfg(any(feature = "esplora-blocking", feature = "esplora-async", feature = "electrum"))]
2
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;
10
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;
19
20 use std::env;
21 use std::sync::Mutex;
22 use std::time::Duration;
23 use std::collections::{HashMap, HashSet};
24
25 pub fn setup_bitcoind_and_electrsd() -> (BitcoinD, ElectrsD) {
26         let bitcoind_exe =
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",
29                 );
30         let mut bitcoind_conf = bitcoind::Conf::default();
31         bitcoind_conf.network = "regtest";
32         let bitcoind = BitcoinD::with_conf(bitcoind_exe, &bitcoind_conf).unwrap();
33
34         let electrs_exe = env::var("ELECTRS_EXE")
35                 .ok()
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();
42         (bitcoind, electrsd)
43 }
44
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
48                 .client
49                 .get_new_address(Some("test"), Some(AddressType::Legacy))
50                 .expect("failed to get new address")
51                 .assume_checked();
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);
55 }
56
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() {
60                 Ok(header) => header,
61                 Err(_) => {
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")
67                 }
68         };
69         loop {
70                 if header.height >= min_height {
71                         break;
72                 }
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")
77                 });
78         }
79 }
80
81 fn exponential_backoff_poll<T, F>(mut poll: F) -> T
82 where
83         F: FnMut() -> Option<T>,
84 {
85         let mut delay = Duration::from_millis(64);
86         let mut tries = 0;
87         loop {
88                 match poll() {
89                         Some(data) => break data,
90                         None if delay.as_millis() < 512 => {
91                                 delay = delay.mul_f32(2.0);
92                                 tries += 1;
93                         }
94                         None if tries == 10 => panic!("Exceeded our maximum wait time."),
95                         None => tries += 1,
96                 }
97
98                 std::thread::sleep(delay);
99         }
100 }
101
102 #[derive(Debug)]
103 enum TestConfirmableEvent {
104         Confirmed(Txid, BlockHash, u32),
105         Unconfirmed(Txid),
106         BestBlockUpdated(BlockHash, u32),
107 }
108
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>>,
114 }
115
116 impl TestConfirmable {
117         pub fn new() -> Self {
118                 let genesis_hash = genesis_block(Network::Regtest).block_hash();
119                 Self {
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()),
124                 }
125         }
126 }
127
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));
136                 }
137         }
138
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));
143         }
144
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));
149         }
150
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<_>>()
153         }
154 }
155
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);
160
161                 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
162                 assert_eq!($confirmable.best_block.lock().unwrap().1, 102);
163
164                 let events = std::mem::take(&mut *$confirmable.events.lock().unwrap());
165                 assert_eq!(events.len(), 1);
166
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());
175
176                 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
177
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());
182
183                 generate_blocks_and_wait(&$bitcoind, &$electrsd, 1);
184                 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
185
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());
190
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()
197                         .unwrap();
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
203                 };
204
205                 $tx_sync.register_output(output);
206                 maybe_await!($tx_sync.sync(vec![&$confirmable])).unwrap();
207
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());
213
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();
217
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);
224
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();
229
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());
234
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);
239
240                 match events[0] {
241                         TestConfirmableEvent::Unconfirmed(t) => {
242                                 assert!(t == txid || t == second_txid);
243                                 assert!(seen_txids.insert(t));
244                         },
245                         _ => panic!("Unexpected event"),
246                 }
247
248                 match events[1] {
249                         TestConfirmableEvent::Unconfirmed(t) => {
250                                 assert!(t == txid || t == second_txid);
251                                 assert!(seen_txids.insert(t));
252                         },
253                         _ => panic!("Unexpected event"),
254                 }
255
256                 match events[2] {
257                         TestConfirmableEvent::BestBlockUpdated(..) => {},
258                         _ => panic!("Unexpected event"),
259                 }
260
261                 match events[3] {
262                         TestConfirmableEvent::Confirmed(t, _, _) => {
263                                 assert!(t == txid || t == second_txid);
264                                 assert!(seen_txids.remove(&t));
265                         },
266                         _ => panic!("Unexpected event"),
267                 }
268
269                 match events[4] {
270                         TestConfirmableEvent::Confirmed(t, _, _) => {
271                                 assert!(t == txid || t == second_txid);
272                                 assert!(seen_txids.remove(&t));
273                         },
274                         _ => panic!("Unexpected event"),
275                 }
276
277                 assert_eq!(seen_txids.len(), 0);
278         }};
279 }
280
281 #[test]
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();
290
291         test_syncing!(tx_sync, confirmable, bitcoind, electrsd);
292 }
293
294 #[tokio::test]
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();
303
304         test_syncing!(tx_sync, confirmable, bitcoind, electrsd);
305 }
306
307 #[test]
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);
317 }