|
| 1 | +#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))] |
1 | 2 | use crate::esplora::EsploraSyncClient;
|
2 | 3 | use lightning::chain::{Confirm, Filter};
|
3 | 4 | use lightning::chain::transaction::TransactionData;
|
@@ -150,10 +151,15 @@ impl Confirm for TestConfirmable {
|
150 | 151 | pub struct TestLogger {}
|
151 | 152 |
|
152 | 153 | impl Logger for TestLogger {
|
153 |
| - fn log(&self, _record: &Record) {} |
| 154 | + fn log(&self, record: &Record) { |
| 155 | + println!("{} -- {}", |
| 156 | + record.level, |
| 157 | + record.args); |
| 158 | + } |
154 | 159 | }
|
155 | 160 |
|
156 | 161 | #[test]
|
| 162 | +#[cfg(feature = "esplora-blocking")] |
157 | 163 | fn test_esplora_syncs() {
|
158 | 164 | premine();
|
159 | 165 | let mut logger = TestLogger {};
|
@@ -236,3 +242,88 @@ fn test_esplora_syncs() {
|
236 | 242 | _ => panic!("Unexpected event"),
|
237 | 243 | }
|
238 | 244 | }
|
| 245 | + |
| 246 | +#[tokio::test] |
| 247 | +#[cfg(feature = "esplora-async")] |
| 248 | +async fn test_esplora_syncs() { |
| 249 | + premine(); |
| 250 | + let mut logger = TestLogger {}; |
| 251 | + let esplora_url = format!("http://{}", get_electrsd().esplora_url.as_ref().unwrap()); |
| 252 | + let tx_sync = EsploraSyncClient::new(esplora_url, &mut logger); |
| 253 | + let confirmable = TestConfirmable::new(); |
| 254 | + |
| 255 | + // Check we pick up on new best blocks |
| 256 | + let expected_height = 0u32; |
| 257 | + assert_eq!(confirmable.best_block.lock().unwrap().1, expected_height); |
| 258 | + |
| 259 | + tx_sync.sync(vec![&confirmable]).await.unwrap(); |
| 260 | + |
| 261 | + let expected_height = get_bitcoind().client.get_block_count().unwrap() as u32; |
| 262 | + assert_eq!(confirmable.best_block.lock().unwrap().1, expected_height); |
| 263 | + |
| 264 | + let events = std::mem::take(&mut *confirmable.events.lock().unwrap()); |
| 265 | + assert_eq!(events.len(), 1); |
| 266 | + |
| 267 | + // Check registered confirmed transactions are marked confirmed |
| 268 | + let new_address = get_bitcoind().client.get_new_address(Some("test"), Some(AddressType::Legacy)).unwrap(); |
| 269 | + let txid = get_bitcoind().client.send_to_address(&new_address, Amount::from_sat(5000), None, None, None, None, None, None).unwrap(); |
| 270 | + tx_sync.register_tx(&txid, &new_address.script_pubkey()); |
| 271 | + |
| 272 | + tx_sync.sync(vec![&confirmable]).await.unwrap(); |
| 273 | + |
| 274 | + let events = std::mem::take(&mut *confirmable.events.lock().unwrap()); |
| 275 | + assert_eq!(events.len(), 0); |
| 276 | + assert!(confirmable.confirmed_txs.lock().unwrap().is_empty()); |
| 277 | + assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty()); |
| 278 | + |
| 279 | + generate_blocks_and_wait(1); |
| 280 | + tx_sync.sync(vec![&confirmable]).await.unwrap(); |
| 281 | + |
| 282 | + let events = std::mem::take(&mut *confirmable.events.lock().unwrap()); |
| 283 | + assert_eq!(events.len(), 2); |
| 284 | + assert!(confirmable.confirmed_txs.lock().unwrap().contains_key(&txid)); |
| 285 | + assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty()); |
| 286 | + |
| 287 | + // Check previously confirmed transactions are marked unconfirmed when they are reorged. |
| 288 | + let best_block_hash = get_bitcoind().client.get_best_block_hash().unwrap(); |
| 289 | + get_bitcoind().client.invalidate_block(&best_block_hash).unwrap(); |
| 290 | + |
| 291 | + // We're getting back to the previous height with a new tip, but best block shouldn't change. |
| 292 | + generate_blocks_and_wait(1); |
| 293 | + assert_ne!(get_bitcoind().client.get_best_block_hash().unwrap(), best_block_hash); |
| 294 | + tx_sync.sync(vec![&confirmable]).await.unwrap(); |
| 295 | + let events = std::mem::take(&mut *confirmable.events.lock().unwrap()); |
| 296 | + assert_eq!(events.len(), 0); |
| 297 | + |
| 298 | + // Now we're surpassing previous height, getting new tip. |
| 299 | + generate_blocks_and_wait(1); |
| 300 | + assert_ne!(get_bitcoind().client.get_best_block_hash().unwrap(), best_block_hash); |
| 301 | + tx_sync.sync(vec![&confirmable]).await.unwrap(); |
| 302 | + |
| 303 | + // Transaction still confirmed but under new tip. |
| 304 | + assert!(confirmable.confirmed_txs.lock().unwrap().contains_key(&txid)); |
| 305 | + assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty()); |
| 306 | + |
| 307 | + // Check we got unconfirmed, then reconfirmed in the meantime. |
| 308 | + let events = std::mem::take(&mut *confirmable.events.lock().unwrap()); |
| 309 | + assert_eq!(events.len(), 3); |
| 310 | + |
| 311 | + match events[0] { |
| 312 | + TestConfirmableEvent::Unconfirmed(t) => { |
| 313 | + assert_eq!(t, txid); |
| 314 | + }, |
| 315 | + _ => panic!("Unexpected event"), |
| 316 | + } |
| 317 | + |
| 318 | + match events[1] { |
| 319 | + TestConfirmableEvent::BestBlockUpdated(..) => {}, |
| 320 | + _ => panic!("Unexpected event"), |
| 321 | + } |
| 322 | + |
| 323 | + match events[2] { |
| 324 | + TestConfirmableEvent::Confirmed(t, _, _) => { |
| 325 | + assert_eq!(t, txid); |
| 326 | + }, |
| 327 | + _ => panic!("Unexpected event"), |
| 328 | + } |
| 329 | +} |
0 commit comments