Skip to content

Commit aeca688

Browse files
committed
test: (V1) Make test cases better
1 parent 97492f8 commit aeca688

File tree

11 files changed

+1256
-303
lines changed

11 files changed

+1256
-303
lines changed

src/maker/server.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,12 @@ fn handle_client(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(), Maker
412412
}
413413
Err(err) => {
414414
match &err {
415-
// Shutdown server if special behavior is set
416415
MakerError::SpecialBehaviour(sp) => {
417416
log::error!(
418-
"[{}] Maker Special Behavior : {:?}",
417+
"[{}] Maker Special Behavior Triggered Disconnection : {:?}",
419418
maker.config.network_port,
420419
sp
421420
);
422-
maker.shutdown.store(true, Relaxed);
423421
}
424422
e => {
425423
log::error!(
@@ -429,7 +427,7 @@ fn handle_client(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(), Maker
429427
);
430428
}
431429
}
432-
return Err(err);
430+
break;
433431
}
434432
}
435433
}

tests/abort1.rs

Lines changed: 113 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use test_framework::*;
2222
/// The Taker after coming live again will see unfinished coinswaps in his wallet. He can reclaim his funds via
2323
/// broadcasting his contract transactions and claiming via timelock.
2424
#[test]
25-
fn test_stop_taker_after_setup() {
25+
fn taker_abort1() {
2626
// ---- Setup ----
2727

2828
// 2 Makers with Normal behavior.
@@ -130,41 +130,124 @@ fn test_stop_taker_after_setup() {
130130
taker.recover_from_swap().unwrap();
131131

132132
// ## Fee Tracking and Workflow:
133-
//
134-
// ### Fee Breakdown:
135-
//
136-
// +------------------+-------------------------+--------------------------+------------+----------------------------+-------------------+
137-
// | Participant | Amount Received (Sats) | Amount Forwarded (Sats) | Fee (Sats) | Funding Mining Fees (Sats) | Total Fees (Sats) |
138-
// +------------------+-------------------------+--------------------------+------------+----------------------------+-------------------+
139-
// | Taker | _ | 500,000 | _ | 3,000 | 3,000 |
140-
// | Maker16102 | 500,000 | 463,500 | 33,500 | 3,000 | 36,500 |
141-
// | Maker6102 | 463,500 | 438,642 | 21,858 | 3,000 | 24,858 |
142-
// +------------------+-------------------------+--------------------------+------------+----------------------------+-------------------+
143-
//
144-
//
145133
// **Taker** => DropConnectionAfterFullSetup
146134
//
147-
// Participants regain their initial funding amounts but incur a total loss of **6,768 sats**
148-
// due to mining fees (recovery + initial transaction fees).
135+
// Participants regain their initial funding amounts but incur a total loss of **858 sats**
136+
// due to mining fees (timelock recovery fee).
149137
//
150138
// ### Recovery Fees Breakdown:
139+
// | Participant | Mining Fee for Contract txes (Sats) | Timelock Fee (Sats) | Funding Fee (Sats)| Total Recovery Fees (Sats) |
140+
// |----------------|------------------------------------|---------------------|--------------------|----------------------------|
141+
// | **Taker** | - | 858 | - | 858 |
142+
// | **Maker6102** | - | 858 | - | 858 |
143+
// | **Maker6102** | - | 858 | - | 858 |
151144
//
152-
// +------------------+------------------------------------+---------------------+--------------------+----------------------------+
153-
// | Participant | Mining Fee for Contract txes (Sats) | Timelock Fee (Sats) | Funding Fee (Sats) | Total Recovery Fees (Sats) |
154-
// +------------------+------------------------------------+---------------------+--------------------+----------------------------+
155-
// | Taker | 3,000 | 768 | 3,000 | 6,768 |
156-
// | Maker16102 | 3,000 | 768 | 3,000 | 6,768 |
157-
// | Maker6102 | 3,000 | 768 | 3,000 | 6,768 |
158-
// +------------------+------------------------------------+---------------------+--------------------+----------------------------+
159-
//
160-
161145
info!("📊 Verifying swap results after taker recovery");
162-
verify_swap_results(
163-
taker,
164-
&makers,
165-
org_taker_spend_balance,
166-
org_maker_spend_balances,
167-
);
146+
// Check Taker balances
147+
{
148+
let wallet = taker.get_wallet();
149+
let balances = wallet.get_balances().unwrap();
150+
151+
// Debug logging for taker
152+
log::info!(
153+
"🔍 DEBUG Taker - Regular: {}, Swap: {}, Spendable: {},Contract: {}",
154+
balances.regular.to_btc(),
155+
balances.swap.to_btc(),
156+
balances.spendable.to_btc(),
157+
balances.contract.to_btc()
158+
);
159+
assert_in_range!(
160+
balances.regular.to_sat(),
161+
[
162+
14999142,// Recover via timelock
163+
],
164+
"Taker seed balance mismatch"
165+
);
166+
167+
assert_in_range!(
168+
balances.swap.to_sat(),
169+
[
170+
0 // No swap happened,each party recovered their outgoing contract via timelock
171+
],
172+
"Taker swapcoin balance mismatch"
173+
);
174+
175+
assert_in_range!(balances.contract.to_sat(), [0], "Contract balance mismatch");
176+
assert_eq!(balances.fidelity, Amount::ZERO);
177+
178+
// Check balance difference
179+
let balance_diff = org_taker_spend_balance
180+
.checked_sub(balances.spendable)
181+
.unwrap();
182+
183+
log::info!(
184+
"🔍 DEBUG Taker balance diff: {} sats",
185+
balance_diff.to_sat()
186+
);
187+
assert_in_range!(
188+
balance_diff.to_sat(),
189+
[
190+
858 // Timlock recovery fee
191+
],
192+
"Taker spendable balance change mismatch"
193+
);
194+
}
195+
196+
// Check Maker balances
197+
makers
198+
.iter()
199+
.zip(org_maker_spend_balances.iter())
200+
.enumerate()
201+
.for_each(|(maker_index, (maker, org_spend_balance))| {
202+
let mut wallet = maker.get_wallet().write().unwrap();
203+
wallet.sync_and_save().unwrap();
204+
let balances = wallet.get_balances().unwrap();
205+
206+
// Debug logging for makers
207+
log::info!(
208+
"🔍 DEBUG Maker {} - Regular: {}, Swap: {}, Contract: {}, Spendable: {}",
209+
maker_index,
210+
balances.regular.to_btc(),
211+
balances.swap.to_btc(),
212+
balances.contract.to_btc(),
213+
balances.spendable.to_btc()
214+
);
215+
216+
assert_in_range!(
217+
balances.regular.to_sat(),
218+
[
219+
14998644, // Makers after recovering via timelock,it has lost some sats here.
220+
],
221+
"Maker seed balance mismatch"
222+
);
223+
224+
assert!(
225+
balances.swap.to_sat() == 0,
226+
"Maker swapcoin balance mismatch"
227+
);
228+
assert_eq!(balances.fidelity, Amount::from_btc(0.05).unwrap());
229+
// Check spendable balance difference.
230+
let balance_diff = match org_spend_balance.checked_sub(balances.spendable) {
231+
None => balances.spendable.checked_sub(*org_spend_balance).unwrap(), // Successful swap as Makers balance increase by Coinswap fee.
232+
Some(diff) => diff, // No spending or unsuccessful swap , Maker may have lost some funds here, generally due to timelock recovery transaction
233+
};
234+
235+
log::info!(
236+
"🔍 DEBUG Maker {} balance diff: {} sats",
237+
maker_index,
238+
balance_diff.to_sat()
239+
);
240+
241+
assert_in_range!(
242+
balance_diff.to_sat(),
243+
[
244+
858,// Maker has lost some sats here due to timelock recovery transaction fee
245+
],
246+
"Maker spendable balance change mismatch"
247+
);
248+
});
249+
250+
log::info!("✅ Swap results verification complete");
168251

169252
info!("🎉 All checks successful. Terminating integration test case");
170253

0 commit comments

Comments
 (0)