1
- #![ cfg( all( not( target_os = "windows" ) , any( feature = "esplora-blocking" , feature = "esplora-async" , feature = "electrum" ) ) ) ]
1
+ #![ cfg( all(
2
+ not( target_os = "windows" ) ,
3
+ any( feature = "esplora-blocking" , feature = "esplora-async" , feature = "electrum" )
4
+ ) ) ]
2
5
3
- #[ cfg( any( feature = "esplora-blocking" , feature = "esplora-async" ) ) ]
4
- use lightning_transaction_sync:: EsploraSyncClient ;
5
- #[ cfg( feature = "electrum" ) ]
6
- use lightning_transaction_sync:: ElectrumSyncClient ;
7
- use lightning:: chain:: { Confirm , Filter , WatchedOutput } ;
8
6
use lightning:: chain:: transaction:: { OutPoint , TransactionData } ;
7
+ use lightning:: chain:: { Confirm , Filter , WatchedOutput } ;
9
8
use lightning:: util:: test_utils:: TestLogger ;
9
+ #[ cfg( feature = "electrum" ) ]
10
+ use lightning_transaction_sync:: ElectrumSyncClient ;
11
+ #[ cfg( any( feature = "esplora-blocking" , feature = "esplora-async" ) ) ]
12
+ use lightning_transaction_sync:: EsploraSyncClient ;
10
13
11
- use electrsd:: { bitcoind, bitcoind:: BitcoinD , ElectrsD } ;
12
- use bitcoin:: { Amount , Txid , BlockHash } ;
14
+ use bdk_macros:: maybe_await;
13
15
use bitcoin:: blockdata:: block:: Header ;
14
16
use bitcoin:: blockdata:: constants:: genesis_block;
15
17
use bitcoin:: network:: Network ;
16
- use electrsd :: bitcoind :: bitcoincore_rpc :: bitcoincore_rpc_json :: AddressType ;
18
+ use bitcoin :: { Amount , BlockHash , Txid } ;
17
19
use bitcoind:: bitcoincore_rpc:: RpcApi ;
18
- use bdk_macros:: maybe_await;
20
+ use electrsd:: bitcoind:: bitcoincore_rpc:: bitcoincore_rpc_json:: AddressType ;
21
+ use electrsd:: { bitcoind, bitcoind:: BitcoinD , ElectrsD } ;
19
22
23
+ use std:: collections:: { HashMap , HashSet } ;
20
24
use std:: env;
21
25
use std:: sync:: Mutex ;
22
26
use std:: time:: Duration ;
23
- use std:: collections:: { HashMap , HashSet } ;
24
27
25
28
pub fn setup_bitcoind_and_electrsd ( ) -> ( BitcoinD , ElectrsD ) {
26
29
let bitcoind_exe =
@@ -63,8 +66,11 @@ pub fn wait_for_block(electrsd: &ElectrsD, min_height: usize) {
63
66
// it didn't. Since we can't proceed without subscribing, we try again after a delay
64
67
// and panic if it still fails.
65
68
std:: thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
66
- electrsd. client . block_headers_subscribe_raw ( ) . expect ( "failed to subscribe to block headers" )
67
- }
69
+ electrsd
70
+ . client
71
+ . block_headers_subscribe_raw ( )
72
+ . expect ( "failed to subscribe to block headers" )
73
+ } ,
68
74
} ;
69
75
loop {
70
76
if header. height >= min_height {
90
96
None if delay. as_millis ( ) < 512 => {
91
97
delay = delay. mul_f32 ( 2.0 ) ;
92
98
tries += 1 ;
93
- }
99
+ } ,
94
100
None if tries == 10 => panic ! ( "Exceeded our maximum wait time." ) ,
95
101
None => tries += 1 ,
96
102
}
@@ -132,7 +138,8 @@ impl Confirm for TestConfirmable {
132
138
let block_hash = header. block_hash ( ) ;
133
139
self . confirmed_txs . lock ( ) . unwrap ( ) . insert ( txid, ( block_hash, height) ) ;
134
140
self . unconfirmed_txs . lock ( ) . unwrap ( ) . remove ( & txid) ;
135
- self . events . lock ( ) . unwrap ( ) . push ( TestConfirmableEvent :: Confirmed ( txid, block_hash, height) ) ;
141
+ let event = TestConfirmableEvent :: Confirmed ( txid, block_hash, height) ;
142
+ self . events . lock ( ) . unwrap ( ) . push ( event) ;
136
143
}
137
144
}
138
145
@@ -145,11 +152,13 @@ impl Confirm for TestConfirmable {
145
152
fn best_block_updated ( & self , header : & Header , height : u32 ) {
146
153
let block_hash = header. block_hash ( ) ;
147
154
* self . best_block . lock ( ) . unwrap ( ) = ( block_hash, height) ;
148
- self . events . lock ( ) . unwrap ( ) . push ( TestConfirmableEvent :: BestBlockUpdated ( block_hash, height) ) ;
155
+ let event = TestConfirmableEvent :: BestBlockUpdated ( block_hash, height) ;
156
+ self . events . lock ( ) . unwrap ( ) . push ( event) ;
149
157
}
150
158
151
159
fn get_relevant_txids ( & self ) -> Vec < ( Txid , u32 , Option < BlockHash > ) > {
152
- self . confirmed_txs . lock ( ) . unwrap ( ) . iter ( ) . map ( |( & txid, ( hash, height) ) | ( txid, * height, Some ( * hash) ) ) . collect :: < Vec < _ > > ( )
160
+ let lock = self . confirmed_txs . lock ( ) . unwrap ( ) ;
161
+ lock. iter ( ) . map ( |( & txid, ( hash, height) ) | ( txid, * height, Some ( * hash) ) ) . collect ( )
153
162
}
154
163
}
155
164
@@ -165,12 +174,37 @@ macro_rules! test_syncing {
165
174
assert_eq!( events. len( ) , 1 ) ;
166
175
167
176
// Check registered confirmed transactions are marked confirmed
168
- let new_address = $bitcoind. client. get_new_address( Some ( "test" ) ,
169
- Some ( AddressType :: Legacy ) ) . unwrap( ) . assume_checked( ) ;
170
- let txid = $bitcoind. client. send_to_address( & new_address, Amount :: from_sat( 5000 ) , None , None ,
171
- None , None , None , None ) . unwrap( ) ;
172
- let second_txid = $bitcoind. client. send_to_address( & new_address, Amount :: from_sat( 5000 ) , None ,
173
- None , None , None , None , None ) . unwrap( ) ;
177
+ let new_address = $bitcoind
178
+ . client
179
+ . get_new_address( Some ( "test" ) , Some ( AddressType :: Legacy ) )
180
+ . unwrap( )
181
+ . assume_checked( ) ;
182
+ let txid = $bitcoind
183
+ . client
184
+ . send_to_address(
185
+ & new_address,
186
+ Amount :: from_sat( 5000 ) ,
187
+ None ,
188
+ None ,
189
+ None ,
190
+ None ,
191
+ None ,
192
+ None ,
193
+ )
194
+ . unwrap( ) ;
195
+ let second_txid = $bitcoind
196
+ . client
197
+ . send_to_address(
198
+ & new_address,
199
+ Amount :: from_sat( 5000 ) ,
200
+ None ,
201
+ None ,
202
+ None ,
203
+ None ,
204
+ None ,
205
+ None ,
206
+ )
207
+ . unwrap( ) ;
174
208
$tx_sync. register_tx( & txid, & new_address. payload( ) . script_pubkey( ) ) ;
175
209
176
210
maybe_await!( $tx_sync. sync( vec![ & $confirmable] ) ) . unwrap( ) ;
@@ -193,13 +227,17 @@ macro_rules! test_syncing {
193
227
let block_hash = tx_res. info. blockhash. unwrap( ) ;
194
228
let tx = tx_res. transaction( ) . unwrap( ) ;
195
229
let prev_outpoint = tx. input. first( ) . unwrap( ) . previous_output;
196
- let prev_tx = $bitcoind. client. get_transaction( & prev_outpoint. txid, None ) . unwrap( ) . transaction( )
230
+ let prev_tx = $bitcoind
231
+ . client
232
+ . get_transaction( & prev_outpoint. txid, None )
233
+ . unwrap( )
234
+ . transaction( )
197
235
. unwrap( ) ;
198
236
let prev_script_pubkey = prev_tx. output[ prev_outpoint. vout as usize ] . script_pubkey. clone( ) ;
199
237
let output = WatchedOutput {
200
238
block_hash: Some ( block_hash) ,
201
239
outpoint: OutPoint { txid: prev_outpoint. txid, index: prev_outpoint. vout as u16 } ,
202
- script_pubkey: prev_script_pubkey
240
+ script_pubkey: prev_script_pubkey,
203
241
} ;
204
242
205
243
$tx_sync. register_output( output) ;
0 commit comments