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