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" ) ) ]
24use lightning_transaction_sync:: EsploraSyncClient ;
5+ #[ cfg( feature = "electrum" ) ]
6+ use lightning_transaction_sync:: ElectrumSyncClient ;
37use lightning:: chain:: { Confirm , Filter , WatchedOutput } ;
48use lightning:: chain:: transaction:: { OutPoint , TransactionData } ;
59use lightning:: util:: test_utils:: TestLogger ;
@@ -10,7 +14,6 @@ use bitcoin::blockdata::constants::genesis_block;
1014use bitcoin:: network:: constants:: Network ;
1115use electrsd:: bitcoind:: bitcoincore_rpc:: bitcoincore_rpc_json:: AddressType ;
1216use bitcoind:: bitcoincore_rpc:: RpcApi ;
13- use electrum_client:: ElectrumApi ;
1417
1518use std:: env;
1619use std:: sync:: Mutex ;
@@ -49,6 +52,7 @@ pub fn generate_blocks_and_wait(bitcoind: &BitcoinD, electrsd: &ElectrsD, num: u
4952}
5053
5154pub 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,141 @@ 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 an arbitrary output of the second 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 , None ) . unwrap ( ) . transaction ( )
472+ . unwrap ( ) ;
473+ let prev_script_pubkey = prev_tx. output [ prev_outpoint. vout as usize ] . script_pubkey . clone ( ) ;
474+ let output = WatchedOutput {
475+ block_hash : Some ( block_hash) ,
476+ outpoint : OutPoint { txid : prev_outpoint. txid , index : prev_outpoint. vout as u16 } ,
477+ script_pubkey : prev_script_pubkey
478+ } ;
479+
480+ tx_sync. register_output ( output) ;
481+ tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
482+
483+ let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
484+ assert_eq ! ( events. len( ) , 1 ) ;
485+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & second_txid) ) ;
486+ assert_eq ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . len( ) , 2 ) ;
487+ assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
488+
489+ // Check previously confirmed transactions are marked unconfirmed when they are reorged.
490+ let best_block_hash = bitcoind. client . get_best_block_hash ( ) . unwrap ( ) ;
491+ bitcoind. client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
492+
493+ // We're getting back to the previous height with a new tip, but best block shouldn't change.
494+ generate_blocks_and_wait ( & bitcoind, & electrsd, 1 ) ;
495+ assert_ne ! ( bitcoind. client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
496+ tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
497+ let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
498+ assert_eq ! ( events. len( ) , 0 ) ;
499+
500+ // Now we're surpassing previous height, getting new tip.
501+ generate_blocks_and_wait ( & bitcoind, & electrsd, 1 ) ;
502+ assert_ne ! ( bitcoind. client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
503+ tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
504+
505+ // Transactions still confirmed but under new tip.
506+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & txid) ) ;
507+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & second_txid) ) ;
508+ assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
509+
510+ // Check we got unconfirmed, then reconfirmed in the meantime.
511+ let mut seen_txids = HashSet :: new ( ) ;
512+ let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
513+ assert_eq ! ( events. len( ) , 5 ) ;
514+
515+ match events[ 0 ] {
516+ TestConfirmableEvent :: Unconfirmed ( t) => {
517+ assert ! ( t == txid || t == second_txid) ;
518+ assert ! ( seen_txids. insert( t) ) ;
519+ } ,
520+ _ => panic ! ( "Unexpected event" ) ,
521+ }
522+
523+ match events[ 1 ] {
524+ TestConfirmableEvent :: Unconfirmed ( t) => {
525+ assert ! ( t == txid || t == second_txid) ;
526+ assert ! ( seen_txids. insert( t) ) ;
527+ } ,
528+ _ => panic ! ( "Unexpected event" ) ,
529+ }
530+
531+ match events[ 2 ] {
532+ TestConfirmableEvent :: BestBlockUpdated ( ..) => { } ,
533+ _ => panic ! ( "Unexpected event" ) ,
534+ }
535+
536+ match events[ 3 ] {
537+ TestConfirmableEvent :: Confirmed ( t, _, _) => {
538+ assert ! ( t == txid || t == second_txid) ;
539+ assert ! ( seen_txids. remove( & t) ) ;
540+ } ,
541+ _ => panic ! ( "Unexpected event" ) ,
542+ }
543+
544+ match events[ 4 ] {
545+ TestConfirmableEvent :: Confirmed ( t, _, _) => {
546+ assert ! ( t == txid || t == second_txid) ;
547+ assert ! ( seen_txids. remove( & t) ) ;
548+ } ,
549+ _ => panic ! ( "Unexpected event" ) ,
550+ }
551+
552+ assert_eq ! ( seen_txids. len( ) , 0 ) ;
553+ }
0 commit comments