1
1
#![ cfg( any( feature = "esplora-blocking" , feature = "esplora-async" ) ) ]
2
2
use lightning_transaction_sync:: EsploraSyncClient ;
3
- use lightning:: chain:: { Confirm , Filter } ;
4
- use lightning:: chain:: transaction:: TransactionData ;
3
+ use lightning:: chain:: { Confirm , Filter , WatchedOutput } ;
4
+ use lightning:: chain:: transaction:: { OutPoint , TransactionData } ;
5
5
use lightning:: util:: test_utils:: TestLogger ;
6
6
7
7
use electrsd:: { bitcoind, bitcoind:: BitcoinD , ElectrsD } ;
@@ -168,6 +168,7 @@ fn test_esplora_syncs() {
168
168
// Check registered confirmed transactions are marked confirmed
169
169
let new_address = bitcoind. client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
170
170
let txid = bitcoind. client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
171
+ let second_txid = bitcoind. client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
171
172
tx_sync. register_tx ( & txid, & new_address. script_pubkey ( ) ) ;
172
173
173
174
tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
@@ -185,6 +186,24 @@ fn test_esplora_syncs() {
185
186
assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & txid) ) ;
186
187
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
187
188
189
+ // Now take a random output of the second transaction and check we'll confirm its spend.
190
+ let tx_res = bitcoind. client . get_transaction ( & second_txid, None ) . unwrap ( ) ;
191
+ let block_hash = tx_res. info . blockhash . unwrap ( ) ;
192
+ let tx = tx_res. transaction ( ) . unwrap ( ) ;
193
+ let prev_outpoint = tx. input . first ( ) . unwrap ( ) . previous_output ;
194
+ let prev_tx = bitcoind. client . get_transaction ( & prev_outpoint. txid , None ) . unwrap ( ) . transaction ( ) . unwrap ( ) ;
195
+ let prev_script_pubkey = prev_tx. output [ prev_outpoint. vout as usize ] . script_pubkey . clone ( ) ;
196
+ let output = WatchedOutput { block_hash : Some ( block_hash) , outpoint : OutPoint { txid : prev_outpoint. txid , index : prev_outpoint. vout as u16 } , script_pubkey : prev_script_pubkey } ;
197
+
198
+ tx_sync. register_output ( output) ;
199
+ tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
200
+
201
+ let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
202
+ assert_eq ! ( events. len( ) , 1 ) ;
203
+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & second_txid) ) ;
204
+ assert_eq ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . len( ) , 2 ) ;
205
+ assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
206
+
188
207
// Check previously confirmed transactions are marked unconfirmed when they are reorged.
189
208
let best_block_hash = bitcoind. client . get_best_block_hash ( ) . unwrap ( ) ;
190
209
bitcoind. client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
@@ -201,29 +220,44 @@ fn test_esplora_syncs() {
201
220
assert_ne ! ( bitcoind. client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
202
221
tx_sync. sync ( vec ! [ & confirmable] ) . unwrap ( ) ;
203
222
204
- // Transaction still confirmed but under new tip.
223
+ // Transactions still confirmed but under new tip.
205
224
assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & txid) ) ;
225
+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & second_txid) ) ;
206
226
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
207
227
208
228
// Check we got unconfirmed, then reconfirmed in the meantime.
209
229
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
210
- assert_eq ! ( events. len( ) , 3 ) ;
230
+ assert_eq ! ( events. len( ) , 5 ) ;
211
231
212
232
match events[ 0 ] {
213
233
TestConfirmableEvent :: Unconfirmed ( t) => {
214
- assert_eq ! ( t, txid) ;
234
+ assert ! ( t == txid || t == second_txid ) ;
215
235
} ,
216
236
_ => panic ! ( "Unexpected event" ) ,
217
237
}
218
238
219
239
match events[ 1 ] {
220
- TestConfirmableEvent :: BestBlockUpdated ( ..) => { } ,
240
+ TestConfirmableEvent :: Unconfirmed ( t) => {
241
+ assert ! ( t == txid || t == second_txid) ;
242
+ } ,
221
243
_ => panic ! ( "Unexpected event" ) ,
222
244
}
223
245
224
246
match events[ 2 ] {
247
+ TestConfirmableEvent :: BestBlockUpdated ( ..) => { } ,
248
+ _ => panic ! ( "Unexpected event" ) ,
249
+ }
250
+
251
+ match events[ 3 ] {
252
+ TestConfirmableEvent :: Confirmed ( t, _, _) => {
253
+ assert ! ( t == txid || t == second_txid) ;
254
+ } ,
255
+ _ => panic ! ( "Unexpected event" ) ,
256
+ }
257
+
258
+ match events[ 4 ] {
225
259
TestConfirmableEvent :: Confirmed ( t, _, _) => {
226
- assert_eq ! ( t, txid) ;
260
+ assert ! ( t == txid || t == second_txid ) ;
227
261
} ,
228
262
_ => panic ! ( "Unexpected event" ) ,
229
263
}
@@ -251,6 +285,7 @@ async fn test_esplora_syncs() {
251
285
// Check registered confirmed transactions are marked confirmed
252
286
let new_address = bitcoind. client . get_new_address ( Some ( "test" ) , Some ( AddressType :: Legacy ) ) . unwrap ( ) ;
253
287
let txid = bitcoind. client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
288
+ let second_txid = bitcoind. client . send_to_address ( & new_address, Amount :: from_sat ( 5000 ) , None , None , None , None , None , None ) . unwrap ( ) ;
254
289
tx_sync. register_tx ( & txid, & new_address. script_pubkey ( ) ) ;
255
290
256
291
tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
@@ -268,6 +303,24 @@ async fn test_esplora_syncs() {
268
303
assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & txid) ) ;
269
304
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
270
305
306
+ // Now take a random output of the second transaction and check we'll confirm its spend.
307
+ let tx_res = bitcoind. client . get_transaction ( & second_txid, None ) . unwrap ( ) ;
308
+ let block_hash = tx_res. info . blockhash . unwrap ( ) ;
309
+ let tx = tx_res. transaction ( ) . unwrap ( ) ;
310
+ let prev_outpoint = tx. input . first ( ) . unwrap ( ) . previous_output ;
311
+ let prev_tx = bitcoind. client . get_transaction ( & prev_outpoint. txid , None ) . unwrap ( ) . transaction ( ) . unwrap ( ) ;
312
+ let prev_script_pubkey = prev_tx. output [ prev_outpoint. vout as usize ] . script_pubkey . clone ( ) ;
313
+ let output = WatchedOutput { block_hash : Some ( block_hash) , outpoint : OutPoint { txid : prev_outpoint. txid , index : prev_outpoint. vout as u16 } , script_pubkey : prev_script_pubkey } ;
314
+
315
+ tx_sync. register_output ( output) ;
316
+ tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
317
+
318
+ let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
319
+ assert_eq ! ( events. len( ) , 1 ) ;
320
+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & second_txid) ) ;
321
+ assert_eq ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . len( ) , 2 ) ;
322
+ assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
323
+
271
324
// Check previously confirmed transactions are marked unconfirmed when they are reorged.
272
325
let best_block_hash = bitcoind. client . get_best_block_hash ( ) . unwrap ( ) ;
273
326
bitcoind. client . invalidate_block ( & best_block_hash) . unwrap ( ) ;
@@ -284,29 +337,44 @@ async fn test_esplora_syncs() {
284
337
assert_ne ! ( bitcoind. client. get_best_block_hash( ) . unwrap( ) , best_block_hash) ;
285
338
tx_sync. sync ( vec ! [ & confirmable] ) . await . unwrap ( ) ;
286
339
287
- // Transaction still confirmed but under new tip.
340
+ // Transactions still confirmed but under new tip.
288
341
assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & txid) ) ;
342
+ assert ! ( confirmable. confirmed_txs. lock( ) . unwrap( ) . contains_key( & second_txid) ) ;
289
343
assert ! ( confirmable. unconfirmed_txs. lock( ) . unwrap( ) . is_empty( ) ) ;
290
344
291
345
// Check we got unconfirmed, then reconfirmed in the meantime.
292
346
let events = std:: mem:: take ( & mut * confirmable. events . lock ( ) . unwrap ( ) ) ;
293
- assert_eq ! ( events. len( ) , 3 ) ;
347
+ assert_eq ! ( events. len( ) , 5 ) ;
294
348
295
349
match events[ 0 ] {
296
350
TestConfirmableEvent :: Unconfirmed ( t) => {
297
- assert_eq ! ( t, txid) ;
351
+ assert ! ( t == txid || t == second_txid ) ;
298
352
} ,
299
353
_ => panic ! ( "Unexpected event" ) ,
300
354
}
301
355
302
356
match events[ 1 ] {
303
- TestConfirmableEvent :: BestBlockUpdated ( ..) => { } ,
357
+ TestConfirmableEvent :: Unconfirmed ( t) => {
358
+ assert ! ( t == txid || t == second_txid) ;
359
+ } ,
304
360
_ => panic ! ( "Unexpected event" ) ,
305
361
}
306
362
307
363
match events[ 2 ] {
364
+ TestConfirmableEvent :: BestBlockUpdated ( ..) => { } ,
365
+ _ => panic ! ( "Unexpected event" ) ,
366
+ }
367
+
368
+ match events[ 3 ] {
369
+ TestConfirmableEvent :: Confirmed ( t, _, _) => {
370
+ assert ! ( t == txid || t == second_txid) ;
371
+ } ,
372
+ _ => panic ! ( "Unexpected event" ) ,
373
+ }
374
+
375
+ match events[ 4 ] {
308
376
TestConfirmableEvent :: Confirmed ( t, _, _) => {
309
- assert_eq ! ( t, txid) ;
377
+ assert ! ( t == txid || t == second_txid ) ;
310
378
} ,
311
379
_ => panic ! ( "Unexpected event" ) ,
312
380
}
0 commit comments