Skip to content

Commit e8b871b

Browse files
committed
test: added few taproot integration test case
1 parent 3470061 commit e8b871b

File tree

8 files changed

+935
-4
lines changed

8 files changed

+935
-4
lines changed

src/bin/taker.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ fn main() -> Result<(), TakerError> {
200200
args.tor_auth,
201201
args.zmq,
202202
None,
203+
#[cfg(feature = "integration-test")]
204+
TakerBehavior::Normal,
203205
)?;
204206
taproot_taker.recover_from_swap()?;
205207
}
@@ -214,6 +216,8 @@ fn main() -> Result<(), TakerError> {
214216
args.tor_auth,
215217
args.zmq,
216218
None,
219+
#[cfg(feature = "integration-test")]
220+
TakerBehavior::Normal,
217221
)?;
218222

219223
let taproot_swap_params = coinswap::taker::api2::SwapParams {

src/maker/api2.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,20 @@ impl Maker {
918918
.contract_tx
919919
.compute_txid();
920920

921+
for (vout, _) in connection_state
922+
.incoming_contract
923+
.contract_tx
924+
.output
925+
.iter()
926+
.enumerate()
927+
{
928+
let outpoint = OutPoint {
929+
txid: incoming_txid,
930+
vout: vout as u32,
931+
};
932+
self.watch_service.unwatch(outpoint);
933+
}
934+
921935
// Record the swept coin to track swap balance
922936
let output_scriptpubkey = spending_tx.output[0].script_pubkey.clone();
923937
// [TODO] Look into the key value pair later, it shouldn't be both sriptpubkey
@@ -926,6 +940,9 @@ impl Maker {
926940
.swept_incoming_swapcoins
927941
.insert(output_scriptpubkey.clone(), output_scriptpubkey);
928942

943+
let mut conn_state = self.ongoing_swap_state.lock()?;
944+
conn_state.remove(connection_state.incoming_contract.swap_id.as_ref().unwrap());
945+
929946
wallet.remove_incoming_swapcoin_v2(&incoming_txid);
930947
log::info!(
931948
"[{}] Removed incoming swapcoin {} after successful sweep",

src/maker/server2.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! It uses the new message protocol (messages2) and integrates with api2.rs and handlers2.rs.
66
//! The server listens at two ports: 6102 for P2P, and 6103 for RPC Client requests.
77
8-
use bitcoin::Amount;
8+
use bitcoin::{Amount, OutPoint};
99
use bitcoind::bitcoincore_rpc::RpcApi;
1010
use std::{
1111
io::ErrorKind,
@@ -474,7 +474,23 @@ fn handle_client_taproot(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(
474474
.outgoing_contract
475475
.contract_tx
476476
.compute_txid();
477+
for (vout, _) in connection_state
478+
.outgoing_contract
479+
.contract_tx
480+
.output
481+
.iter()
482+
.enumerate()
483+
{
484+
let outpoint = OutPoint {
485+
txid: outgoing_txid,
486+
vout: vout as u32,
487+
};
488+
maker.watch_service.unwatch(outpoint);
489+
}
477490
{
491+
let mut conn_state = maker.ongoing_swap_state.lock()?;
492+
conn_state
493+
.remove(connection_state.outgoing_contract.swap_id.as_ref().unwrap());
478494
let mut wallet = maker.wallet.write()?;
479495
wallet.remove_outgoing_swapcoin_v2(&outgoing_txid);
480496
log::info!(

src/taker/api2.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use super::error::TakerError;
2+
#[cfg(feature = "integration-test")]
3+
use crate::taker::TakerBehavior;
14
use 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)]
5859
pub 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

325328
impl 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

Comments
 (0)