Skip to content

Commit 6619ef8

Browse files
committed
f Also test async version
1 parent 05517f1 commit 6619ef8

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lightning-transaction-sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ esplora-client = { version = "0.3.0", default-features = false, optional = true
3030
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
3131
electrum-client = "0.12.0"
3232
once_cell = "1.16.0"
33+
tokio = { version = "1.14.0", features = ["full"] }

lightning-transaction-sync/src/tests.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
12
use crate::esplora::EsploraSyncClient;
23
use lightning::chain::{Confirm, Filter};
34
use lightning::chain::transaction::TransactionData;
@@ -150,10 +151,15 @@ impl Confirm for TestConfirmable {
150151
pub struct TestLogger {}
151152

152153
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+
}
154159
}
155160

156161
#[test]
162+
#[cfg(feature = "esplora-blocking")]
157163
fn test_esplora_syncs() {
158164
premine();
159165
let mut logger = TestLogger {};
@@ -236,3 +242,88 @@ fn test_esplora_syncs() {
236242
_ => panic!("Unexpected event"),
237243
}
238244
}
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

Comments
 (0)