Skip to content

Commit 32833f2

Browse files
committed
Add Electrum integration test
1 parent 787d9a7 commit 32833f2

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

ci/ci-tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ if [[ $RUSTC_MINOR_VERSION -gt 67 && "$HOST_PLATFORM" != *windows* ]]; then
6868
cargo check --verbose --color always --features esplora-async
6969
cargo test --verbose --color always --features esplora-async-https
7070
cargo check --verbose --color always --features esplora-async-https
71+
cargo test --verbose --color always --features electrum
72+
cargo check --verbose --color always --features electrum
7173
popd
7274
fi
7375

lightning-transaction-sync/tests/integration_tests.rs

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
#![cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
1+
#![cfg(any(feature = "esplora-blocking", feature = "esplora-async", feature = "electrum"))]
2+
3+
#[cfg(any(feature = "esplora-blocking", feature = "esplora-async"))]
24
use lightning_transaction_sync::EsploraSyncClient;
5+
#[cfg(feature = "electrum")]
6+
use lightning_transaction_sync::ElectrumSyncClient;
37
use lightning::chain::{Confirm, Filter, WatchedOutput};
48
use lightning::chain::transaction::{OutPoint, TransactionData};
59
use lightning::util::test_utils::TestLogger;
@@ -10,7 +14,6 @@ use bitcoin::blockdata::constants::genesis_block;
1014
use bitcoin::network::constants::Network;
1115
use electrsd::bitcoind::bitcoincore_rpc::bitcoincore_rpc_json::AddressType;
1216
use bitcoind::bitcoincore_rpc::RpcApi;
13-
use electrum_client::ElectrumApi;
1417

1518
use std::env;
1619
use std::sync::Mutex;
@@ -49,6 +52,7 @@ pub fn generate_blocks_and_wait(bitcoind: &BitcoinD, electrsd: &ElectrsD, num: u
4952
}
5053

5154
pub fn wait_for_block(electrsd: &ElectrsD, min_height: usize) {
55+
use electrsd::electrum_client::ElectrumApi;
5256
let mut header = match electrsd.client.block_headers_subscribe() {
5357
Ok(header) => header,
5458
Err(_) => {
@@ -409,3 +413,132 @@ async fn test_esplora_syncs() {
409413

410414
assert_eq!(seen_txids.len(), 0);
411415
}
416+
417+
#[test]
418+
#[cfg(feature = "electrum")]
419+
fn test_electrum_syncs() {
420+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
421+
generate_blocks_and_wait(&bitcoind, &electrsd, 101);
422+
let mut logger = TestLogger::new();
423+
let electrum_url = format!("tcp://{}", electrsd.electrum_url);
424+
let tx_sync = match ElectrumSyncClient::new(electrum_url, &mut logger) {
425+
Ok(tx_sync) => tx_sync,
426+
Err(e) => {
427+
eprintln!("{:?}", e);
428+
panic!("{:?}", e);
429+
}
430+
};
431+
let confirmable = TestConfirmable::new();
432+
433+
// Check we pick up on new best blocks
434+
assert_eq!(confirmable.best_block.lock().unwrap().1, 0);
435+
436+
tx_sync.sync(vec![&confirmable]).unwrap();
437+
assert_eq!(confirmable.best_block.lock().unwrap().1, 102);
438+
439+
let events = std::mem::take(&mut *confirmable.events.lock().unwrap());
440+
assert_eq!(events.len(), 1);
441+
442+
// Check registered confirmed transactions are marked confirmed
443+
let new_address = bitcoind.client.get_new_address(Some("test"),
444+
Some(AddressType::Legacy)).unwrap();
445+
let txid = bitcoind.client.send_to_address(&new_address, Amount::from_sat(5000), None, None,
446+
None, None, None, None).unwrap();
447+
let second_txid = bitcoind.client.send_to_address(&new_address, Amount::from_sat(5000), None,
448+
None, None, None, None, None).unwrap();
449+
tx_sync.register_tx(&txid, &new_address.script_pubkey());
450+
451+
tx_sync.sync(vec![&confirmable]).unwrap();
452+
453+
let events = std::mem::take(&mut *confirmable.events.lock().unwrap());
454+
assert_eq!(events.len(), 0);
455+
assert!(confirmable.confirmed_txs.lock().unwrap().is_empty());
456+
assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty());
457+
458+
generate_blocks_and_wait(&bitcoind, &electrsd, 1);
459+
tx_sync.sync(vec![&confirmable]).unwrap();
460+
461+
let events = std::mem::take(&mut *confirmable.events.lock().unwrap());
462+
assert_eq!(events.len(), 2);
463+
assert!(confirmable.confirmed_txs.lock().unwrap().contains_key(&txid));
464+
assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty());
465+
466+
// Now take a random output of the first transaction and check we'll confirm its spend.
467+
let tx_res = bitcoind.client.get_transaction(&second_txid, None).unwrap();
468+
let block_hash = tx_res.info.blockhash.unwrap();
469+
let tx = tx_res.transaction().unwrap();
470+
let prev_outpoint = tx.input.first().unwrap().previous_output;
471+
let prev_tx = bitcoind.client.get_transaction(&prev_outpoint.txid,
472+
None).unwrap().transaction().unwrap();
473+
let prev_script_pubkey = prev_tx.output[prev_outpoint.vout as usize].script_pubkey.clone();
474+
let output = WatchedOutput { block_hash: Some(block_hash), outpoint: OutPoint {
475+
txid: prev_outpoint.txid, index: prev_outpoint.vout as u16 },
476+
script_pubkey: prev_script_pubkey };
477+
478+
tx_sync.register_output(output);
479+
tx_sync.sync(vec![&confirmable]).unwrap();
480+
481+
let events = std::mem::take(&mut *confirmable.events.lock().unwrap());
482+
assert_eq!(events.len(), 1);
483+
assert!(confirmable.confirmed_txs.lock().unwrap().contains_key(&second_txid));
484+
assert_eq!(confirmable.confirmed_txs.lock().unwrap().len(), 2);
485+
assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty());
486+
487+
// Check previously confirmed transactions are marked unconfirmed when they are reorged.
488+
let best_block_hash = bitcoind.client.get_best_block_hash().unwrap();
489+
bitcoind.client.invalidate_block(&best_block_hash).unwrap();
490+
491+
// We're getting back to the previous height with a new tip, but best block shouldn't change.
492+
generate_blocks_and_wait(&bitcoind, &electrsd, 1);
493+
assert_ne!(bitcoind.client.get_best_block_hash().unwrap(), best_block_hash);
494+
tx_sync.sync(vec![&confirmable]).unwrap();
495+
let events = std::mem::take(&mut *confirmable.events.lock().unwrap());
496+
assert_eq!(events.len(), 0);
497+
498+
// Now we're surpassing previous height, getting new tip.
499+
generate_blocks_and_wait(&bitcoind, &electrsd, 1);
500+
assert_ne!(bitcoind.client.get_best_block_hash().unwrap(), best_block_hash);
501+
tx_sync.sync(vec![&confirmable]).unwrap();
502+
503+
// Transactions still confirmed but under new tip.
504+
assert!(confirmable.confirmed_txs.lock().unwrap().contains_key(&txid));
505+
assert!(confirmable.confirmed_txs.lock().unwrap().contains_key(&second_txid));
506+
assert!(confirmable.unconfirmed_txs.lock().unwrap().is_empty());
507+
508+
// Check we got unconfirmed, then reconfirmed in the meantime.
509+
let events = std::mem::take(&mut *confirmable.events.lock().unwrap());
510+
assert_eq!(events.len(), 5);
511+
512+
match events[0] {
513+
TestConfirmableEvent::Unconfirmed(t) => {
514+
assert!(t == txid || t == second_txid);
515+
},
516+
_ => panic!("Unexpected event"),
517+
}
518+
519+
match events[1] {
520+
TestConfirmableEvent::Unconfirmed(t) => {
521+
assert!(t == txid || t == second_txid);
522+
},
523+
_ => panic!("Unexpected event"),
524+
}
525+
526+
match events[2] {
527+
TestConfirmableEvent::BestBlockUpdated(..) => {},
528+
_ => panic!("Unexpected event"),
529+
}
530+
531+
match events[3] {
532+
TestConfirmableEvent::Confirmed(t, _, _) => {
533+
assert!(t == txid || t == second_txid);
534+
},
535+
_ => panic!("Unexpected event"),
536+
}
537+
538+
match events[4] {
539+
TestConfirmableEvent::Confirmed(t, _, _) => {
540+
assert!(t == txid || t == second_txid);
541+
},
542+
_ => panic!("Unexpected event"),
543+
}
544+
}

0 commit comments

Comments
 (0)