11use crate :: tests:: test_utils:: expect_event;
22use crate :: { Builder , Config , Error , Event } ;
33
4- use bitcoin:: { Address , Amount } ;
4+ use bitcoin:: { Address , Amount , Txid } ;
55use bitcoind:: bitcoincore_rpc:: RpcApi ;
66use electrsd:: bitcoind:: bitcoincore_rpc:: bitcoincore_rpc_json:: AddressType ;
77use electrsd:: { bitcoind, bitcoind:: BitcoinD , ElectrsD } ;
@@ -69,6 +69,20 @@ fn wait_for_block(min_height: usize) {
6969 }
7070}
7171
72+ fn wait_for_tx ( txid : Txid ) {
73+ let mut tx_res = get_electrsd ( ) . client . transaction_get ( & txid) ;
74+ loop {
75+ if tx_res. is_ok ( ) {
76+ break ;
77+ }
78+ tx_res = exponential_backoff_poll ( || {
79+ get_electrsd ( ) . trigger ( ) . unwrap ( ) ;
80+ get_electrsd ( ) . client . ping ( ) . unwrap ( ) ;
81+ Some ( get_electrsd ( ) . client . transaction_get ( & txid) )
82+ } ) ;
83+ }
84+ }
85+
7286fn exponential_backoff_poll < T , F > ( mut poll : F ) -> T
7387where
7488 F : FnMut ( ) -> Option < T > ,
@@ -91,10 +105,11 @@ fn premine_and_distribute_funds(addrs: Vec<Address>, amount: Amount) {
91105 } ) ;
92106
93107 for addr in addrs {
94- get_bitcoind ( )
108+ let txid = get_bitcoind ( )
95109 . client
96110 . send_to_address ( & addr, amount, None , None , None , None , None , None )
97111 . unwrap ( ) ;
112+ wait_for_tx ( txid) ;
98113 }
99114
100115 generate_blocks_and_wait ( 1 ) ;
@@ -147,8 +162,17 @@ fn channel_full_cycle() {
147162 format ! ( "{}@{}" , node_b. node_id( ) . unwrap( ) , node_b. listening_address( ) . unwrap( ) ) ;
148163 node_a. connect_open_channel ( & node_b_addr, 50000 , true ) . unwrap ( ) ;
149164
150- // Wait a sec so the funding tx can 'propagate' via EsploraD to BitcoinD.
151- std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
165+ let funding_txo = loop {
166+ let details = node_a. list_channels ( ) ;
167+
168+ if details. is_empty ( ) || details[ 0 ] . funding_txo . is_none ( ) {
169+ std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
170+ } else {
171+ break details[ 0 ] . funding_txo . unwrap ( ) ;
172+ }
173+ } ;
174+
175+ wait_for_tx ( funding_txo. txid ) ;
152176
153177 println ! ( "\n .. generating blocks, syncing wallets .. " ) ;
154178 generate_blocks_and_wait ( 6 ) ;
0 commit comments