Skip to content

Commit

Permalink
Added more asserts in IT
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Mar 22, 2024
1 parent bfeaf1d commit 03bb7ed
Show file tree
Hide file tree
Showing 5 changed files with 610 additions and 167 deletions.
141 changes: 119 additions & 22 deletions tests/abort1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use coinswap::{

mod test_framework;
use log::{info, warn};
use std::{sync::Arc, thread, time::Duration};
use std::{assert_eq, sync::Arc, thread, time::Duration};
use test_framework::*;

/// Abort 1: TAKER Drops After Full Setup.
Expand Down Expand Up @@ -87,18 +87,31 @@ async fn test_stop_taker_after_setup() {
let mut all_utxos = taker.read().unwrap().get_wallet().get_all_utxo().unwrap();

// Get the original balances
let org_taker_balance = taker
let org_taker_balance_fidelity = taker
.read()
.unwrap()
.get_wallet()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let org_taker_balance_descriptor_utxo = taker
.read()
.unwrap()
.get_wallet()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let org_taker_balance_swap_coins = taker
.read()
.unwrap()
+ taker
.read()
.unwrap()
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let org_taker_balance_live_contract = taker
.read()
.unwrap()
.get_wallet()
.balance_live_contract(Some(&all_utxos))
.unwrap();
let org_taker_balance = org_taker_balance_descriptor_utxo + org_taker_balance_swap_coins;

// ---- Start Servers and attempt Swap ----

Expand Down Expand Up @@ -132,18 +145,38 @@ async fn test_stop_taker_after_setup() {
.iter()
.map(|maker| {
all_utxos = maker.get_wallet().read().unwrap().get_all_utxo().unwrap();
maker
let maker_balance_fidelity = maker
.get_wallet()
.read()
.unwrap()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let maker_balance_descriptor_utxo = maker
.get_wallet()
.read()
.unwrap()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let maker_balance_swap_coins = maker
.get_wallet()
.read()
.unwrap()
+ maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let maker_balance_live_contract = maker
.get_wallet()
.read()
.unwrap()
.balance_live_contract(Some(&all_utxos))
.unwrap();
assert_eq!(maker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
maker_balance_descriptor_utxo,
Amount::from_btc(0.14999).unwrap()
);
assert_eq!(maker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(maker_balance_live_contract, Amount::from_btc(0.0).unwrap());
maker_balance_descriptor_utxo + maker_balance_swap_coins
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -185,25 +218,80 @@ async fn test_stop_taker_after_setup() {
all_utxos = taker.read().unwrap().get_wallet().get_all_utxo().unwrap();

// Check everybody looses mining fees of contract txs.
let taker_balance = taker
let taker_balance_fidelity = taker
.read()
.unwrap()
.get_wallet()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let taker_balance_descriptor_utxo = taker
.read()
.unwrap()
.get_wallet()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let taker_balance_swap_coins = taker
.read()
.unwrap()
+ taker
.read()
.unwrap()
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let taker_balance_live_contract = taker
.read()
.unwrap()
.get_wallet()
.balance_live_contract(Some(&all_utxos))
.unwrap();
let taker_balance = taker_balance_descriptor_utxo + taker_balance_swap_coins;

assert_eq!(org_taker_balance - taker_balance, Amount::from_sat(4227));
assert_eq!(org_taker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
org_taker_balance_descriptor_utxo,
Amount::from_btc(0.15).unwrap()
);
assert_eq!(org_taker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(
org_taker_balance_live_contract,
Amount::from_btc(0.0).unwrap()
);
assert_eq!(taker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
taker_balance_descriptor_utxo,
Amount::from_btc(0.14995773).unwrap()
);
assert_eq!(taker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(taker_balance_live_contract, Amount::from_btc(0.0).unwrap());

makers
.iter()
.zip(org_maker_balances.iter())
.for_each(|(maker, org_balance)| {
all_utxos = maker.get_wallet().read().unwrap().get_all_utxo().unwrap();
let maker_balance_fidelity = maker
.get_wallet()
.read()
.unwrap()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let maker_balance_descriptor_utxo = maker
.get_wallet()
.read()
.unwrap()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let maker_balance_swap_coins = maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let maker_balance_live_contract = maker
.get_wallet()
.read()
.unwrap()
.balance_live_contract(Some(&all_utxos))
.unwrap();
let new_balance = maker
.get_wallet()
.read()
Expand All @@ -216,7 +304,16 @@ async fn test_stop_taker_after_setup() {
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();

assert_eq!(*org_balance - new_balance, Amount::from_sat(4227));

assert_eq!(maker_balance_fidelity, Amount::from_btc(0.05).unwrap());
assert_eq!(
maker_balance_descriptor_utxo,
Amount::from_btc(0.14994773).unwrap()
);
assert_eq!(maker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(maker_balance_live_contract, Amount::from_btc(0.0).unwrap());
});

info!("All checks successful. Terminating integration test case");
Expand Down
129 changes: 95 additions & 34 deletions tests/abort2_case2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ async fn test_abort_case_2_recover_if_no_makers_found() {
let mut all_utxos = taker.read().unwrap().get_wallet().get_all_utxo().unwrap();

// Get the original balances
let org_taker_balance = taker
let org_taker_balance_descriptor_utxo = taker
.read()
.unwrap()
.get_wallet()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let org_taker_balance_swap_coins = taker
.read()
.unwrap()
+ taker
.read()
.unwrap()
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();

let org_taker_balance = org_taker_balance_descriptor_utxo + org_taker_balance_swap_coins;

// ---- Start Servers and attempt Swap ----

Expand Down Expand Up @@ -132,18 +134,46 @@ async fn test_abort_case_2_recover_if_no_makers_found() {
.iter()
.map(|maker| {
all_utxos = maker.get_wallet().read().unwrap().get_all_utxo().unwrap();
maker
let maker_balance_fidelity = maker
.get_wallet()
.read()
.unwrap()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let maker_balance_descriptor_utxo = maker
.get_wallet()
.read()
.unwrap()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let maker_balance_swap_coins = maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let maker_balance_live_contract = maker
.get_wallet()
.read()
.unwrap()
+ maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap()
.balance_live_contract(Some(&all_utxos))
.unwrap();

assert_eq!(maker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
maker_balance_descriptor_utxo,
Amount::from_btc(0.14999).unwrap()
);
assert_eq!(maker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(maker_balance_live_contract, Amount::from_btc(0.0).unwrap());

(
maker_balance_fidelity,
maker_balance_descriptor_utxo,
maker_balance_swap_coins,
maker_balance_live_contract,
maker_balance_descriptor_utxo + maker_balance_swap_coins,
)
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -188,41 +218,72 @@ async fn test_abort_case_2_recover_if_no_makers_found() {

// Assert that Taker burned the mining fees,
// Makers are fine.
let new_taker_balance = taker

let new_taker_balance_descriptor_utxo = taker
.read()
.unwrap()
.get_wallet()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let new_taker_balance_swap_coins = taker
.read()
.unwrap()
+ taker
.read()
.unwrap()
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
assert_eq!(
org_taker_balance - new_taker_balance,
Amount::from_sat(4227)
);
.get_wallet()
.balance_swap_coins(Some(&all_utxos))
.unwrap();

let new_taker_balance = new_taker_balance_descriptor_utxo + new_taker_balance_swap_coins;

// Balance will not differ if the first maker drops and swap doesn't take place.
// The recovery will happen only if the 2nd maker drops, which has 50% probabiltiy.
// Only do this assert if the balance differs, implying that the swap took place.
if new_taker_balance != org_taker_balance {
assert_eq!(
org_taker_balance - new_taker_balance,
Amount::from_sat(4227)
);
}
makers
.iter()
.zip(org_maker_balances.iter())
.for_each(|(maker, org_balance)| {
all_utxos = maker.get_wallet().read().unwrap().get_all_utxo().unwrap();

let new_balance = maker
let maker_balance_fidelity = maker
.get_wallet()
.read()
.unwrap()
.balance_fidelity_bonds(Some(&all_utxos))
.unwrap();
let maker_balance_descriptor_utxo = maker
.get_wallet()
.read()
.unwrap()
.balance_descriptor_utxo(Some(&all_utxos))
.unwrap();
let maker_balance_swap_coins = maker
.get_wallet()
.read()
.unwrap()
+ maker
.get_wallet()
.read()
.unwrap()
.balance_swap_coins(Some(&all_utxos))
.unwrap();
assert_eq!(*org_balance - new_balance, Amount::from_sat(0));
.balance_swap_coins(Some(&all_utxos))
.unwrap();
let maker_balance_live_contract = maker
.get_wallet()
.read()
.unwrap()
.balance_live_contract(Some(&all_utxos))
.unwrap();

let new_balance = maker_balance_descriptor_utxo + maker_balance_swap_coins;

assert_eq!(org_balance.4 - new_balance, Amount::from_sat(0));

assert_eq!(maker_balance_fidelity, Amount::from_btc(0.0).unwrap());
assert_eq!(
maker_balance_descriptor_utxo,
Amount::from_btc(0.14999000).unwrap()
);
assert_eq!(maker_balance_swap_coins, Amount::from_btc(0.0).unwrap());
assert_eq!(maker_balance_live_contract, Amount::from_btc(0.0).unwrap());
});

// Stop test and clean everything.
Expand Down
Loading

0 comments on commit 03bb7ed

Please sign in to comment.