11#![ allow( unused) ]
2- use anyhow:: Context ;
2+ use anyhow:: { bail , Context } ;
33use bdk_electrum:: electrum_client:: ElectrumApi ;
44use bdk_electrum:: { electrum_client, BdkElectrumClient } ;
55use bdk_sqlx:: sqlx:: Postgres ;
@@ -162,26 +162,24 @@ async fn main() -> anyhow::Result<()> {
162162 . await
163163 {
164164 Ok ( _) => {
165- println ! (
166- "ERROR: Double-spend succeeded! This should not happen."
167- ) ;
165+ println ! ( "Double-spend succeeded!" ) ;
168166 }
169167 Err ( e) => {
170168 println ! ( "✓ Double-spend correctly failed: {}" , e) ;
171169 }
172170 }
173171 }
174172 Err ( e) => {
175- println ! ( "Failed to create second transaction: {}" , e) ;
173+ bail ! ( "Failed to create second transaction: {}" , e) ;
176174 }
177175 }
178176 }
179177 }
180- Err ( e) => println ! ( "Failed to create transaction: {}" , e) ,
178+ Err ( e) => bail ! ( "Failed to create transaction: {}" , e) ,
181179 }
182180 } else {
183181 println ! ( "\n Insufficient balance to create a transaction. Need at least 10,000 sats." ) ;
184- println ! ( "Current balance: {} sats" , balance. total( ) . to_sat( ) ) ;
182+ bail ! ( "Current balance: {} sats" , balance. total( ) . to_sat( ) ) ;
185183 }
186184
187185 // Load wallet 2
@@ -229,9 +227,7 @@ fn electrum(
229227) -> anyhow:: Result < ( ) > {
230228 let client = BdkElectrumClient :: new ( electrum_client:: Client :: new ( electrum_url) ?) ;
231229 let request = wallet. start_full_scan ( ) . build ( ) ;
232- let res = client
233- . full_scan :: < _ > ( request, STOP_GAP , BATCH_SIZE , true )
234- . context ( "scanning the blockchain" ) ?;
230+ let res = client. full_scan :: < _ > ( request, STOP_GAP , BATCH_SIZE , true ) ?;
235231 wallet. apply_update ( res) ?;
236232 Ok ( ( ) )
237233}
@@ -335,7 +331,7 @@ async fn create_rbf_transaction(
335331 let txid = tx. clone ( ) . compute_txid ( ) ;
336332
337333 if !finalized {
338- return Err ( anyhow :: anyhow !( "Failed to finalize transaction" ) ) ;
334+ bail ! ( "Failed to finalize transaction" ) ;
339335 }
340336
341337 // Extract the signed transaction
@@ -357,7 +353,7 @@ async fn create_rbf_transaction(
357353 }
358354 Err ( e) => {
359355 println ! ( "Failed to broadcast: {}" , e) ;
360- Err ( anyhow :: anyhow !( "Broadcast failed: {}" , e) )
356+ bail ! ( "Broadcast failed: {}" , e)
361357 }
362358 }
363359}
@@ -380,7 +376,7 @@ async fn verify_transaction_persisted(
380376 let wallet = match Wallet :: load ( ) . load_wallet_async ( & mut store) . await ? {
381377 Some ( wallet) => wallet,
382378 None => {
383- return Err ( anyhow :: anyhow !( "Failed to load wallet from persistence" ) ) ;
379+ bail ! ( "Failed to load wallet from persistence" ) ;
384380 }
385381 } ;
386382
@@ -401,7 +397,7 @@ async fn verify_transaction_persisted(
401397 }
402398 } else {
403399 println ! ( "\n ✗ Transaction NOT found in persisted wallet!" ) ;
404- return Err ( anyhow :: anyhow !( "Transaction not found after persistence" ) ) ;
400+ bail ! ( "Transaction not found after persistence" ) ;
405401 }
406402
407403 Ok ( ( ) )
@@ -506,7 +502,7 @@ async fn create_coincontrol_transaction(
506502 let finalized = wallet. sign ( & mut psbt, SignOptions :: default ( ) ) ?;
507503
508504 if !finalized {
509- return Err ( anyhow :: anyhow !( "Failed to finalize transaction" ) ) ;
505+ bail ! ( "Failed to finalize transaction" ) ;
510506 }
511507
512508 let tx = psbt. extract_tx ( ) . expect ( "valid tx" ) ;
@@ -528,7 +524,7 @@ async fn create_coincontrol_transaction(
528524 }
529525 Err ( e) => {
530526 println ! ( "Broadcast failed (expected for double-spend): {}" , e) ;
531- Err ( anyhow :: anyhow !( "Broadcast failed: {}" , e) )
527+ bail ! ( "Broadcast failed: {}" , e)
532528 }
533529 }
534530}
@@ -566,9 +562,7 @@ async fn create_double_spend_transaction(
566562
567563 let finalized = wallet. sign ( & mut psbt, SignOptions :: default ( ) ) ?;
568564 if !finalized {
569- return Err ( anyhow:: anyhow!(
570- "Failed to finalize double-spend transaction"
571- ) ) ;
565+ bail ! ( "Failed to finalize double-spend transaction" ) ;
572566 }
573567
574568 let tx = psbt. extract_tx ( ) . expect ( "valid tx" ) ;
@@ -581,15 +575,12 @@ async fn create_double_spend_transaction(
581575
582576 match client. transaction_broadcast_raw ( & tx_raw) {
583577 Ok ( _) => Ok ( txid) ,
584- Err ( e) => Err ( anyhow :: anyhow !( "Broadcast failed: {}" , e) ) ,
578+ Err ( e) => bail ! ( "Broadcast failed: {}" , e) ,
585579 }
586580 }
587581 Err ( e) => {
588582 // This is the expected path
589- Err ( anyhow:: anyhow!(
590- "Transaction building failed (expected): {}" ,
591- e
592- ) )
583+ bail ! ( "Transaction building failed (expected): {}" , e)
593584 }
594585 }
595586}
@@ -660,10 +651,14 @@ async fn test_spk_cache_performance(url: &str) -> anyhow::Result<()> {
660651
661652 // Measure loading time
662653 let load_start = Instant :: now ( ) ;
663- let loaded_wallet = match Wallet :: load ( ) . load_wallet_async ( & mut store) . await ? {
654+ let loaded_wallet = match Wallet :: load ( )
655+ . use_spk_cache ( true )
656+ . load_wallet_async ( & mut store)
657+ . await ?
658+ {
664659 Some ( wallet) => wallet,
665660 None => {
666- return Err ( anyhow :: anyhow !( "Failed to load wallet from persistence" ) ) ;
661+ bail ! ( "Failed to load wallet from persistence" ) ;
667662 }
668663 } ;
669664 let load_time = load_start. elapsed ( ) ;
0 commit comments