mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Refactor punish test and punish assertions
This commit is contained in:
parent
73a2841ec5
commit
55024572ae
@ -1,21 +1,5 @@
|
|||||||
use crate::testutils::{init_alice, init_bob};
|
|
||||||
use futures::{
|
|
||||||
future::{join, select, Either},
|
|
||||||
FutureExt,
|
|
||||||
};
|
|
||||||
use get_port::get_port;
|
|
||||||
use libp2p::Multiaddr;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use swap::{
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
bitcoin,
|
|
||||||
config::Config,
|
|
||||||
monero,
|
|
||||||
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use testcontainers::clients::Cli;
|
|
||||||
use testutils::init_tracing;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod testutils;
|
pub mod testutils;
|
||||||
|
|
||||||
@ -23,128 +7,64 @@ pub mod testutils;
|
|||||||
/// the encsig and fail to refund or redeem. Alice punishes.
|
/// the encsig and fail to refund or redeem. Alice punishes.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
async fn alice_punishes_if_bob_never_acts_after_fund() {
|
||||||
let _guard = init_tracing();
|
testutils::test(|alice_harness, bob_harness| async move {
|
||||||
|
let alice = alice_harness.new_alice().await;
|
||||||
|
let bob = bob_harness.new_bob().await;
|
||||||
|
|
||||||
let cli = Cli::default();
|
let alice_swap = alice::swap(
|
||||||
let (
|
alice.state,
|
||||||
monero,
|
alice.event_loop_handle,
|
||||||
testutils::Containers {
|
alice.bitcoin_wallet.clone(),
|
||||||
bitcoind,
|
alice.monero_wallet.clone(),
|
||||||
monerods: _monerods,
|
alice.config,
|
||||||
},
|
alice.swap_id,
|
||||||
) = testutils::init_containers(&cli).await;
|
alice.db,
|
||||||
|
);
|
||||||
|
let alice_swap_handle = tokio::spawn(alice_swap);
|
||||||
|
|
||||||
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
let bob_state = bob::run_until(
|
||||||
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
bob.state,
|
||||||
|
|
||||||
let bob_btc_starting_balance = btc_to_swap * 10;
|
|
||||||
|
|
||||||
let alice_btc_starting_balance = bitcoin::Amount::ZERO;
|
|
||||||
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
|
||||||
|
|
||||||
let port = get_port().expect("Failed to find a free port");
|
|
||||||
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
|
||||||
.parse()
|
|
||||||
.expect("failed to parse Alice's address");
|
|
||||||
|
|
||||||
let config = Config::regtest();
|
|
||||||
|
|
||||||
let (
|
|
||||||
alice_state,
|
|
||||||
mut alice_event_loop,
|
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet,
|
|
||||||
alice_xmr_wallet,
|
|
||||||
alice_db,
|
|
||||||
) = init_alice(
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
xmr_to_swap,
|
|
||||||
alice_xmr_starting_balance,
|
|
||||||
alice_multiaddr.clone(),
|
|
||||||
config,
|
|
||||||
Seed::random().unwrap(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
|
||||||
init_bob(
|
|
||||||
alice_multiaddr,
|
|
||||||
alice_event_loop.peer_id(),
|
|
||||||
&bitcoind,
|
|
||||||
&monero,
|
|
||||||
btc_to_swap,
|
|
||||||
bob_btc_starting_balance,
|
|
||||||
xmr_to_swap,
|
|
||||||
config,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let bob_btc_locked_fut = bob::swap::run_until(
|
|
||||||
bob_state,
|
|
||||||
bob::swap::is_btc_locked,
|
bob::swap::is_btc_locked,
|
||||||
bob_event_loop_handle,
|
bob.event_loop_handle,
|
||||||
bob_db,
|
bob.db,
|
||||||
bob_btc_wallet.clone(),
|
bob.bitcoin_wallet.clone(),
|
||||||
bob_xmr_wallet.clone(),
|
bob.monero_wallet.clone(),
|
||||||
OsRng,
|
OsRng,
|
||||||
Uuid::new_v4(),
|
bob.swap_id,
|
||||||
)
|
)
|
||||||
.boxed();
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let bob_fut = select(bob_btc_locked_fut, bob_event_loop.run().boxed());
|
assert!(matches!(bob_state, BobState::BtcLocked {..}));
|
||||||
|
|
||||||
let alice_fut = alice::swap::swap(
|
let alice_state = alice_swap_handle.await.unwrap();
|
||||||
alice_state,
|
alice_harness.assert_punished(alice_state.unwrap()).await;
|
||||||
alice_event_loop_handle,
|
|
||||||
alice_btc_wallet.clone(),
|
|
||||||
alice_xmr_wallet.clone(),
|
|
||||||
Config::regtest(),
|
|
||||||
Uuid::new_v4(),
|
|
||||||
alice_db,
|
|
||||||
)
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
let alice_fut = select(alice_fut, alice_event_loop.run().boxed());
|
// Restart Bob after Alice punished to ensure Bob transitions to
|
||||||
|
// punished and does not run indefinitely
|
||||||
|
let bob = bob_harness.recover_bob_from_db().await;
|
||||||
|
assert!(matches!(bob.state, BobState::BtcLocked {..}));
|
||||||
|
|
||||||
// Wait until alice has locked xmr and bob has locked btc
|
// TODO: make lock-tx-id available in final states
|
||||||
let (alice_state, bob_state) = join(alice_fut, bob_fut).await;
|
let lock_tx_id = if let BobState::BtcLocked(state3) = bob_state {
|
||||||
|
state3.tx_lock_id()
|
||||||
let alice_state = match alice_state {
|
|
||||||
Either::Left((state, _)) => state.unwrap(),
|
|
||||||
Either::Right(_) => panic!("Alice event loop should not terminate."),
|
|
||||||
};
|
|
||||||
|
|
||||||
let bob_state = match bob_state {
|
|
||||||
Either::Left((state, _)) => state.unwrap(),
|
|
||||||
Either::Right(_) => panic!("Bob event loop should not terminate."),
|
|
||||||
};
|
|
||||||
|
|
||||||
assert!(matches!(alice_state, AliceState::BtcPunished));
|
|
||||||
let bob_state3 = if let BobState::BtcLocked(state3, ..) = bob_state {
|
|
||||||
state3
|
|
||||||
} else {
|
} else {
|
||||||
panic!("Bob in unexpected state");
|
panic!("Bob in unexpected state");
|
||||||
};
|
};
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
let bob_state = bob::swap(
|
||||||
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();
|
bob.state,
|
||||||
|
bob.event_loop_handle,
|
||||||
// lock_tx_bitcoin_fee is determined by the wallet, it is not necessarily equal
|
bob.db,
|
||||||
// to TX_FEE
|
bob.bitcoin_wallet.clone(),
|
||||||
let lock_tx_bitcoin_fee = bob_btc_wallet
|
bob.monero_wallet.clone(),
|
||||||
.transaction_fee(bob_state3.tx_lock_id())
|
OsRng,
|
||||||
|
bob.swap_id,
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
bob_harness.assert_punished(bob_state, lock_tx_id).await;
|
||||||
btc_alice_final,
|
})
|
||||||
alice_btc_starting_balance + btc_to_swap - bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
|
.await;
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
btc_bob_final,
|
|
||||||
bob_btc_starting_balance - btc_to_swap - lock_tx_bitcoin_fee
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
@ -65,16 +65,29 @@ impl AliceHarness {
|
|||||||
pub async fn assert_redeemed(&self, state: AliceState) {
|
pub async fn assert_redeemed(&self, state: AliceState) {
|
||||||
assert!(matches!(state, AliceState::BtcRedeemed));
|
assert!(matches!(state, AliceState::BtcRedeemed));
|
||||||
|
|
||||||
let btc_alice_final = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
btc_alice_final,
|
btc_balance_after_swap,
|
||||||
self.starting_balances.btc + self.swap_amounts.btc
|
self.starting_balances.btc + self.swap_amounts.btc
|
||||||
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
||||||
);
|
);
|
||||||
|
|
||||||
let xmr_alice_final = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
assert!(xmr_alice_final <= self.starting_balances.xmr - self.swap_amounts.xmr);
|
assert!(xmr_balance_after_swap <= self.starting_balances.xmr - self.swap_amounts.xmr);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn assert_punished(&self, state: AliceState) {
|
||||||
|
assert!(matches!(state, AliceState::BtcPunished));
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
btc_balance_after_swap,
|
||||||
|
self.starting_balances.btc + self.swap_amounts.btc
|
||||||
|
- bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE)
|
||||||
|
);
|
||||||
|
|
||||||
|
let xnr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert!(xnr_balance_after_swap <= self.starting_balances.xmr - self.swap_amounts.xmr);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn new(
|
pub async fn new(
|
||||||
@ -294,15 +307,38 @@ impl BobHarness {
|
|||||||
pub async fn assert_redeemed(&self, state: BobState) {
|
pub async fn assert_redeemed(&self, state: BobState) {
|
||||||
assert!(matches!(state, BobState::XmrRedeemed));
|
assert!(matches!(state, BobState::XmrRedeemed));
|
||||||
|
|
||||||
let btc_bob_final = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert!(btc_balance_after_swap <= self.starting_balances.btc - self.swap_amounts.btc);
|
||||||
|
|
||||||
|
// Ensure that Bob's balance is refreshed as we use a newly created wallet
|
||||||
self.monero_wallet.as_ref().inner.refresh().await.unwrap();
|
self.monero_wallet.as_ref().inner.refresh().await.unwrap();
|
||||||
let xmr_bob_final = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
assert!(btc_bob_final <= self.starting_balances.btc - self.swap_amounts.btc);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
xmr_bob_final,
|
xmr_balance_after_swap,
|
||||||
self.starting_balances.xmr + self.swap_amounts.xmr
|
self.starting_balances.xmr + self.swap_amounts.xmr
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn assert_punished(&self, state: BobState, lock_tx_id: ::bitcoin::Txid) {
|
||||||
|
assert!(matches!(state, BobState::BtcPunished));
|
||||||
|
|
||||||
|
// lock_tx_bitcoin_fee is determined by the wallet, it is not necessarily equal
|
||||||
|
// to TX_FEE
|
||||||
|
let lock_tx_bitcoin_fee = self
|
||||||
|
.bitcoin_wallet
|
||||||
|
.transaction_fee(lock_tx_id)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let btc_balance_after_swap = self.bitcoin_wallet.as_ref().balance().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
btc_balance_after_swap,
|
||||||
|
self.starting_balances.btc - self.swap_amounts.btc - lock_tx_bitcoin_fee
|
||||||
|
);
|
||||||
|
|
||||||
|
let xmr_balance_after_swap = self.monero_wallet.as_ref().get_balance().await.unwrap();
|
||||||
|
assert_eq!(xmr_balance_after_swap, self.starting_balances.xmr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn test<T, F>(testfn: T)
|
pub async fn test<T, F>(testfn: T)
|
||||||
|
Loading…
Reference in New Issue
Block a user