@@ -38,6 +38,7 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
38
38
/// transactions.
39
39
pub fn populate_tx_cache ( & self , txs : impl IntoIterator < Item = impl Into < Arc < Transaction > > > ) {
40
40
let mut tx_cache = self . tx_cache . lock ( ) . unwrap ( ) ;
41
+
41
42
for tx in txs {
42
43
let tx = tx. into ( ) ;
43
44
let txid = tx. compute_txid ( ) ;
@@ -385,11 +386,9 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
385
386
Err ( other_err) => return Err ( other_err) ,
386
387
} ;
387
388
388
- let spk = tx
389
- . output
390
- . first ( )
391
- . map ( |txo| & txo. script_pubkey )
392
- . expect ( "tx must have an output" ) ;
389
+ let Some ( spk) = tx. output . first ( ) . map ( |txo| & txo. script_pubkey ) else {
390
+ continue ;
391
+ } ;
393
392
394
393
// because of restrictions of the Electrum API, we have to use the `script_get_history`
395
394
// call to get confirmation status of our transaction
@@ -475,8 +474,11 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
475
474
let outpoint = vin. previous_output ;
476
475
let vout = outpoint. vout ;
477
476
let prev_tx = self . fetch_tx ( outpoint. txid ) ?;
478
- let txout = prev_tx. output [ vout as usize ] . clone ( ) ;
479
- let _ = tx_update. txouts . insert ( outpoint, txout) ;
477
+ // Only insert txout if it exists. This avoids panicking on malformed or
478
+ // incomplete transactions.
479
+ if let Some ( txout) = prev_tx. output . get ( vout as usize ) {
480
+ let _ = tx_update. txouts . insert ( outpoint, txout. clone ( ) ) ;
481
+ }
480
482
}
481
483
}
482
484
}
@@ -548,11 +550,11 @@ fn fetch_tip_and_latest_blocks(
548
550
} )
549
551
. fold ( agreement_cp, |prev_cp, block| {
550
552
Some ( match prev_cp {
551
- Some ( cp) => cp. push ( block) . expect ( "must extend checkpoint" ) ,
553
+ Some ( cp) => cp. push ( block) . ok ( ) ? ,
552
554
None => CheckPoint :: new ( block) ,
553
555
} )
554
556
} )
555
- . expect ( "must have at least one checkpoint" ) ;
557
+ . ok_or_else ( || Error :: Message ( "failed to construct new checkpoint tip" . to_string ( ) ) ) ? ;
556
558
557
559
Ok ( ( new_tip, new_blocks) )
558
560
}
0 commit comments