2020-12-14 01:24:23 -05:00
|
|
|
use crate::testutils::{init_alice, init_bob};
|
|
|
|
use futures::future::try_join;
|
2020-12-17 19:54:13 -05:00
|
|
|
use get_port::get_port;
|
2020-12-14 01:24:23 -05:00
|
|
|
use libp2p::Multiaddr;
|
|
|
|
use rand::rngs::OsRng;
|
2021-01-04 22:08:36 -05:00
|
|
|
use swap::{
|
|
|
|
bitcoin,
|
|
|
|
config::Config,
|
|
|
|
database::Database,
|
|
|
|
monero,
|
2021-01-07 18:52:07 -05:00
|
|
|
protocol::{alice, alice::AliceState, bob, bob::BobState},
|
2021-01-04 22:08:36 -05:00
|
|
|
};
|
2020-12-14 01:24:23 -05:00
|
|
|
use tempfile::tempdir;
|
|
|
|
use testcontainers::clients::Cli;
|
|
|
|
use testutils::init_tracing;
|
2020-12-17 20:15:53 -05:00
|
|
|
use tokio::select;
|
2020-12-14 01:24:23 -05:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
pub mod testutils;
|
|
|
|
|
|
|
|
// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
|
|
|
|
// then also refunds.
|
|
|
|
#[tokio::test]
|
2020-12-17 19:46:00 -05:00
|
|
|
async fn given_alice_restarts_after_xmr_is_locked_abort_swap() {
|
2020-12-14 01:24:23 -05:00
|
|
|
let _guard = init_tracing();
|
|
|
|
|
|
|
|
let cli = Cli::default();
|
2020-12-14 04:51:20 -05:00
|
|
|
let (
|
|
|
|
monero,
|
|
|
|
testutils::Containers {
|
|
|
|
bitcoind,
|
|
|
|
monerods: _monerods,
|
|
|
|
},
|
|
|
|
) = testutils::init_containers(&cli).await;
|
2020-12-14 01:24:23 -05:00
|
|
|
|
|
|
|
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
2021-01-04 22:08:36 -05:00
|
|
|
let xmr_to_swap = monero::Amount::from_piconero(1_000_000_000_000);
|
2020-12-14 01:24:23 -05:00
|
|
|
|
|
|
|
let bob_btc_starting_balance = btc_to_swap * 10;
|
2021-01-04 22:08:36 -05:00
|
|
|
let bob_xmr_starting_balance = monero::Amount::from_piconero(0);
|
2020-12-14 01:24:23 -05:00
|
|
|
|
|
|
|
let alice_btc_starting_balance = bitcoin::Amount::ZERO;
|
|
|
|
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
|
|
|
|
2020-12-17 19:54:13 -05:00
|
|
|
let port = get_port().expect("Failed to find a free port");
|
|
|
|
let alice_multiaddr: Multiaddr = format!("/ip4/127.0.0.1/tcp/{}", port)
|
2020-12-14 01:24:23 -05:00
|
|
|
.parse()
|
|
|
|
.expect("failed to parse Alice's address");
|
|
|
|
|
|
|
|
let (
|
|
|
|
alice_state,
|
2020-12-17 20:15:53 -05:00
|
|
|
mut alice_event_loop_1,
|
|
|
|
alice_event_loop_handle_1,
|
2020-12-14 01:24:23 -05:00
|
|
|
alice_btc_wallet,
|
|
|
|
alice_xmr_wallet,
|
2020-12-14 06:16:39 -05:00
|
|
|
_,
|
2020-12-14 01:24:23 -05:00
|
|
|
) = init_alice(
|
|
|
|
&bitcoind,
|
|
|
|
&monero,
|
|
|
|
btc_to_swap,
|
|
|
|
xmr_to_swap,
|
|
|
|
alice_xmr_starting_balance,
|
|
|
|
alice_multiaddr.clone(),
|
|
|
|
Config::regtest(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2020-12-14 05:30:45 -05:00
|
|
|
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
2020-12-14 01:24:23 -05:00
|
|
|
init_bob(
|
2020-12-14 05:30:45 -05:00
|
|
|
alice_multiaddr.clone(),
|
2020-12-18 01:39:04 -05:00
|
|
|
alice_event_loop_1.peer_id(),
|
2020-12-14 01:24:23 -05:00
|
|
|
&bitcoind,
|
|
|
|
&monero,
|
|
|
|
btc_to_swap,
|
|
|
|
bob_btc_starting_balance,
|
|
|
|
xmr_to_swap,
|
|
|
|
Config::regtest(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let bob_fut = bob::swap::swap(
|
|
|
|
bob_state,
|
2020-12-14 05:30:45 -05:00
|
|
|
bob_event_loop_handle,
|
2020-12-14 01:24:23 -05:00
|
|
|
bob_db,
|
|
|
|
bob_btc_wallet.clone(),
|
|
|
|
bob_xmr_wallet.clone(),
|
|
|
|
OsRng,
|
|
|
|
Uuid::new_v4(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let alice_swap_id = Uuid::new_v4();
|
|
|
|
let alice_db_datadir = tempdir().unwrap();
|
|
|
|
|
2020-12-17 21:44:19 -05:00
|
|
|
let alice_xmr_locked_fut = {
|
|
|
|
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
|
|
alice::swap::run_until(
|
|
|
|
alice_state,
|
|
|
|
alice::swap::is_xmr_locked,
|
|
|
|
alice_event_loop_handle_1,
|
|
|
|
alice_btc_wallet.clone(),
|
|
|
|
alice_xmr_wallet.clone(),
|
|
|
|
Config::regtest(),
|
|
|
|
alice_swap_id,
|
|
|
|
alice_db,
|
|
|
|
)
|
|
|
|
};
|
2020-12-14 01:24:23 -05:00
|
|
|
|
2020-12-20 22:53:35 -05:00
|
|
|
tokio::spawn(bob_event_loop.run());
|
2020-12-14 01:24:23 -05:00
|
|
|
|
2020-12-17 20:15:53 -05:00
|
|
|
// We are selecting with alice_event_loop_1 so that we stop polling on it once
|
|
|
|
// the try_join is finished.
|
|
|
|
let (bob_state, alice_restart_state) = select! {
|
|
|
|
res = try_join(bob_fut, alice_xmr_locked_fut) => res.unwrap(),
|
|
|
|
_ = alice_event_loop_1.run() => panic!("The event loop should never finish")
|
|
|
|
};
|
2020-12-14 01:24:23 -05:00
|
|
|
|
2020-12-17 21:50:54 -05:00
|
|
|
let tx_lock_id = if let BobState::BtcRefunded(state4) = bob_state {
|
|
|
|
state4.tx_lock_id()
|
2020-12-14 01:24:23 -05:00
|
|
|
} else {
|
|
|
|
panic!("Bob in unexpected state");
|
|
|
|
};
|
|
|
|
|
2020-12-17 20:15:53 -05:00
|
|
|
let (mut alice_event_loop_2, alice_event_loop_handle_2) =
|
2020-12-14 05:48:42 -05:00
|
|
|
testutils::init_alice_event_loop(alice_multiaddr);
|
2020-12-14 01:24:23 -05:00
|
|
|
|
2020-12-17 21:44:19 -05:00
|
|
|
let alice_final_state = {
|
|
|
|
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
|
|
alice::swap::swap(
|
|
|
|
alice_restart_state,
|
|
|
|
alice_event_loop_handle_2,
|
|
|
|
alice_btc_wallet.clone(),
|
|
|
|
alice_xmr_wallet.clone(),
|
|
|
|
Config::regtest(),
|
|
|
|
alice_swap_id,
|
|
|
|
alice_db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
};
|
2020-12-17 20:15:53 -05:00
|
|
|
tokio::spawn(async move { alice_event_loop_2.run().await });
|
2020-12-14 01:24:23 -05:00
|
|
|
|
2020-12-17 20:15:53 -05:00
|
|
|
assert!(matches!(alice_final_state, AliceState::XmrRefunded));
|
2020-12-14 01:24:23 -05:00
|
|
|
|
|
|
|
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
|
|
|
let btc_bob_final = bob_btc_wallet.as_ref().balance().await.unwrap();
|
|
|
|
|
2020-12-17 21:50:54 -05:00
|
|
|
let lock_tx_bitcoin_fee = bob_btc_wallet.transaction_fee(tx_lock_id).await.unwrap();
|
2020-12-14 01:24:23 -05:00
|
|
|
|
|
|
|
assert_eq!(btc_alice_final, alice_btc_starting_balance);
|
|
|
|
|
|
|
|
// Alice or Bob could publish TxCancel. This means Bob could pay tx fees for
|
|
|
|
// TxCancel and TxRefund or only TxRefund
|
|
|
|
let btc_bob_final_alice_submitted_cancel = btc_bob_final
|
|
|
|
== bob_btc_starting_balance
|
|
|
|
- lock_tx_bitcoin_fee
|
|
|
|
- bitcoin::Amount::from_sat(bitcoin::TX_FEE);
|
|
|
|
|
|
|
|
let btc_bob_final_bob_submitted_cancel = btc_bob_final
|
|
|
|
== bob_btc_starting_balance
|
|
|
|
- lock_tx_bitcoin_fee
|
|
|
|
- bitcoin::Amount::from_sat(2 * bitcoin::TX_FEE);
|
|
|
|
assert!(btc_bob_final_alice_submitted_cancel || btc_bob_final_bob_submitted_cancel);
|
|
|
|
|
2021-01-04 04:29:11 -05:00
|
|
|
alice_xmr_wallet.as_ref().inner.refresh().await.unwrap();
|
2020-12-14 01:24:23 -05:00
|
|
|
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
|
|
assert_eq!(xmr_alice_final, xmr_to_swap);
|
|
|
|
|
2021-01-04 04:29:11 -05:00
|
|
|
bob_xmr_wallet.as_ref().inner.refresh().await.unwrap();
|
2020-12-14 01:24:23 -05:00
|
|
|
let xmr_bob_final = bob_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
|
|
assert_eq!(xmr_bob_final, bob_xmr_starting_balance);
|
|
|
|
}
|