1+ use super :: error:: TakerError ;
2+ #[ cfg( feature = "integration-test" ) ]
3+ use crate :: taker:: TakerBehavior ;
14use crate :: {
25 protocol:: {
36 contract2:: {
@@ -51,8 +54,6 @@ use std::{
5154 time:: Duration ,
5255} ;
5356
54- use super :: error:: TakerError ;
55-
5657/// Represents how a taproot contract output was spent
5758#[ derive( Debug , Clone ) ]
5859pub enum TaprootSpendingPath {
@@ -320,6 +321,8 @@ pub struct Taker {
320321 ongoing_swap_state : OngoingSwapState ,
321322 data_dir : PathBuf ,
322323 watch_service : WatchService ,
324+ #[ cfg( feature = "integration-test" ) ]
325+ behavior : TakerBehavior ,
323326}
324327
325328impl Taker {
@@ -332,6 +335,7 @@ impl Taker {
332335 tor_auth_password : Option < String > ,
333336 zmq_addr : String ,
334337 password : Option < String > ,
338+ #[ cfg( feature = "integration-test" ) ] behavior : TakerBehavior ,
335339 ) -> Result < Taker , TakerError > {
336340 let data_dir = data_dir. unwrap_or_else ( get_taker_dir) ;
337341
@@ -417,6 +421,8 @@ impl Taker {
417421 ongoing_swap_state : OngoingSwapState :: default ( ) ,
418422 data_dir,
419423 watch_service,
424+ #[ cfg( feature = "integration-test" ) ]
425+ behavior,
420426 } )
421427 }
422428
@@ -487,6 +493,14 @@ impl Taker {
487493 self . wallet . wait_for_tx_confirmation ( tx. compute_txid ( ) ) ?;
488494 }
489495
496+ #[ cfg( feature = "integration-test" ) ]
497+ {
498+ if self . behavior == TakerBehavior :: DropConnectionAfterFullSetup {
499+ log:: error!( "Dropping Swap Process after full setup" ) ;
500+ self . recover_from_swap ( ) ?;
501+ return Ok ( None ) ;
502+ }
503+ }
490504 match self
491505 . negotiate_with_makers_and_coordinate_sweep ( & outgoing_signed_contract_transactions)
492506 {
@@ -497,6 +511,7 @@ impl Taker {
497511
498512 // Store swap state before reset for report generation
499513 let prereset_swapstate = self . ongoing_swap_state . clone ( ) ;
514+ self . save_and_reset_swap_round ( ) ?;
500515
501516 // Sync wallet and generate report
502517 self . wallet . sync_and_save ( ) ?;
@@ -518,6 +533,44 @@ impl Taker {
518533 }
519534 }
520535
536+ /// Save all the finalized swap data and reset the [`OngoingSwapState`].
537+ fn save_and_reset_swap_round ( & mut self ) -> Result < ( ) , TakerError > {
538+ // Mark incoiming swapcoins as done
539+ let incoming_swapcoin = & self . ongoing_swap_state . incoming_contract ;
540+ let contract_txid = incoming_swapcoin. contract_tx . compute_txid ( ) ;
541+ for ( vout, _) in incoming_swapcoin. contract_tx . output . iter ( ) . enumerate ( ) {
542+ let outpoint = OutPoint {
543+ txid : contract_txid,
544+ vout : vout as u32 ,
545+ } ;
546+ self . watch_service . unwatch ( outpoint) ;
547+ }
548+
549+ // Mark outgoing swapcoins as done.
550+ let outgoing_swapcoin = & self . ongoing_swap_state . outgoing_contract ;
551+ let contract_txid = outgoing_swapcoin. contract_tx . compute_txid ( ) ;
552+ for ( vout, _) in outgoing_swapcoin. contract_tx . output . iter ( ) . enumerate ( ) {
553+ let outpoint = OutPoint {
554+ txid : contract_txid,
555+ vout : vout as u32 ,
556+ } ;
557+ self . watch_service . unwatch ( outpoint) ;
558+ }
559+
560+ self . wallet . sync_no_fail ( ) ;
561+
562+ self . wallet . save_to_disk ( ) ?;
563+
564+ self . clear_ongoing_swaps ( ) ;
565+
566+ Ok ( ( ) )
567+ }
568+
569+ /// Clear the [`OngoingSwapState`].
570+ fn clear_ongoing_swaps ( & mut self ) {
571+ self . ongoing_swap_state = OngoingSwapState :: default ( ) ;
572+ }
573+
521574 /// Generate a swap report after successful completion of a taproot coinswap
522575 fn generate_swap_report (
523576 & self ,
0 commit comments