2020-12-18 00:54:13 +00:00
|
|
|
use crate::testutils::{init_alice, init_bob};
|
|
|
|
use get_port::get_port;
|
2020-12-10 03:59:09 +00:00
|
|
|
use libp2p::Multiaddr;
|
|
|
|
use rand::rngs::OsRng;
|
2020-12-23 04:17:27 +00:00
|
|
|
use swap::{alice, alice::swap::AliceState, bitcoin, bob, database::Database};
|
2020-12-10 03:59:09 +00:00
|
|
|
use tempfile::tempdir;
|
|
|
|
use testcontainers::clients::Cli;
|
2020-12-18 00:54:13 +00:00
|
|
|
use testutils::init_tracing;
|
2020-12-10 03:59:09 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
use xmr_btc::config::Config;
|
|
|
|
|
|
|
|
pub mod testutils;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
|
|
|
let _guard = init_tracing();
|
|
|
|
|
|
|
|
let cli = Cli::default();
|
2020-12-14 09:51:20 +00:00
|
|
|
let (
|
|
|
|
monero,
|
|
|
|
testutils::Containers {
|
|
|
|
bitcoind,
|
|
|
|
monerods: _monerods,
|
|
|
|
},
|
|
|
|
) = testutils::init_containers(&cli).await;
|
2020-12-10 03:59:09 +00:00
|
|
|
|
|
|
|
let btc_to_swap = bitcoin::Amount::from_sat(1_000_000);
|
|
|
|
let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000);
|
|
|
|
|
|
|
|
let bob_btc_starting_balance = btc_to_swap * 10;
|
|
|
|
let alice_xmr_starting_balance = xmr_to_swap * 10;
|
|
|
|
|
2020-12-18 00:54:13 +00: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-10 03:59:09 +00:00
|
|
|
.parse()
|
|
|
|
.expect("failed to parse Alice's address");
|
|
|
|
|
|
|
|
let config = Config::regtest();
|
|
|
|
|
|
|
|
let (
|
2020-12-14 06:24:23 +00:00
|
|
|
start_state,
|
2020-12-10 03:59:09 +00:00
|
|
|
mut alice_event_loop,
|
|
|
|
alice_event_loop_handle,
|
|
|
|
alice_btc_wallet,
|
|
|
|
alice_xmr_wallet,
|
2020-12-14 11:16:39 +00:00
|
|
|
_,
|
2020-12-10 03:59:09 +00:00
|
|
|
) = init_alice(
|
|
|
|
&bitcoind,
|
|
|
|
&monero,
|
|
|
|
btc_to_swap,
|
|
|
|
xmr_to_swap,
|
|
|
|
alice_xmr_starting_balance,
|
|
|
|
alice_multiaddr.clone(),
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2020-12-18 06:39:04 +00:00
|
|
|
let alice_peer_id = alice_event_loop.peer_id();
|
|
|
|
|
2020-12-10 03:59:09 +00:00
|
|
|
let (bob_state, bob_event_loop, bob_event_loop_handle, bob_btc_wallet, bob_xmr_wallet, bob_db) =
|
|
|
|
init_bob(
|
2020-12-14 06:24:23 +00:00
|
|
|
alice_multiaddr.clone(),
|
2020-12-18 06:39:04 +00:00
|
|
|
alice_peer_id.clone(),
|
2020-12-10 03:59:09 +00:00
|
|
|
&bitcoind,
|
|
|
|
&monero,
|
|
|
|
btc_to_swap,
|
|
|
|
bob_btc_starting_balance,
|
|
|
|
xmr_to_swap,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2020-12-15 02:11:16 +00:00
|
|
|
// TODO: we are making a clone of Bob's wallets here to keep them in scope after
|
|
|
|
// Bob's wallets are moved into an async task.
|
|
|
|
let bob_btc_wallet_clone = bob_btc_wallet.clone();
|
|
|
|
let bob_xmr_wallet_clone = bob_xmr_wallet.clone();
|
|
|
|
|
2020-12-21 00:21:53 +00:00
|
|
|
let bob_fut = bob::swap::swap(
|
2020-12-18 06:39:04 +00:00
|
|
|
bob_state,
|
|
|
|
bob_event_loop_handle,
|
|
|
|
bob_db,
|
|
|
|
bob_btc_wallet.clone(),
|
|
|
|
bob_xmr_wallet.clone(),
|
|
|
|
OsRng,
|
|
|
|
Uuid::new_v4(),
|
2020-12-21 00:21:53 +00:00
|
|
|
);
|
2020-12-10 03:59:09 +00:00
|
|
|
|
|
|
|
let alice_db_datadir = tempdir().unwrap();
|
|
|
|
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
|
|
|
2020-12-21 00:21:53 +00:00
|
|
|
tokio::spawn(async move { alice_event_loop.run().await });
|
2020-12-21 03:49:19 +00:00
|
|
|
let bob_swap_handle = tokio::spawn(bob_fut);
|
2020-12-21 00:21:53 +00:00
|
|
|
tokio::spawn(bob_event_loop.run());
|
2020-12-10 03:59:09 +00:00
|
|
|
|
|
|
|
let alice_swap_id = Uuid::new_v4();
|
|
|
|
|
2020-12-14 10:30:45 +00:00
|
|
|
let alice_state = alice::swap::run_until(
|
2020-12-14 06:24:23 +00:00
|
|
|
start_state,
|
2020-12-10 03:59:09 +00:00
|
|
|
alice::swap::is_encsig_learned,
|
|
|
|
alice_event_loop_handle,
|
|
|
|
alice_btc_wallet.clone(),
|
|
|
|
alice_xmr_wallet.clone(),
|
2020-12-14 11:16:39 +00:00
|
|
|
config,
|
2020-12-10 03:59:09 +00:00
|
|
|
alice_swap_id,
|
|
|
|
alice_db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-12-22 03:53:21 +00:00
|
|
|
assert!(matches!(alice_state, AliceState::EncSigLearned {..}));
|
2020-12-10 03:59:09 +00:00
|
|
|
|
|
|
|
let alice_db = Database::open(alice_db_datadir.path()).unwrap();
|
|
|
|
|
2020-12-23 04:30:23 +00:00
|
|
|
let resume_state =
|
|
|
|
if let swap::database::Swap::Alice(state) = alice_db.get_state(alice_swap_id).unwrap() {
|
|
|
|
assert!(matches!(state, swap::database::Alice::EncSigLearned {..}));
|
|
|
|
state.into()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2020-12-14 03:43:05 +00:00
|
|
|
|
2020-12-14 09:51:20 +00:00
|
|
|
let (mut event_loop_after_restart, event_loop_handle_after_restart) =
|
2020-12-14 10:48:42 +00:00
|
|
|
testutils::init_alice_event_loop(alice_multiaddr);
|
2020-12-21 03:53:35 +00:00
|
|
|
tokio::spawn(async move { event_loop_after_restart.run().await });
|
2020-12-14 06:24:23 +00:00
|
|
|
|
2020-12-17 23:50:32 +00:00
|
|
|
let alice_state = alice::swap::swap(
|
|
|
|
resume_state,
|
2020-12-14 06:24:23 +00:00
|
|
|
event_loop_handle_after_restart,
|
2020-12-15 02:11:16 +00:00
|
|
|
alice_btc_wallet.clone(),
|
|
|
|
alice_xmr_wallet.clone(),
|
2020-12-14 11:16:39 +00:00
|
|
|
config,
|
2020-12-14 06:24:23 +00:00
|
|
|
alice_swap_id,
|
2020-12-10 03:59:09 +00:00
|
|
|
alice_db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-12-21 03:49:19 +00:00
|
|
|
// Wait for Bob to finish
|
|
|
|
bob_swap_handle.await.unwrap().unwrap();
|
|
|
|
|
2020-12-10 03:59:09 +00:00
|
|
|
assert!(matches!(alice_state, AliceState::BtcRedeemed {..}));
|
2020-12-14 09:51:20 +00:00
|
|
|
|
2020-12-15 02:11:16 +00:00
|
|
|
let btc_alice_final = alice_btc_wallet.as_ref().balance().await.unwrap();
|
|
|
|
let btc_bob_final = bob_btc_wallet_clone.as_ref().balance().await.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
btc_alice_final,
|
|
|
|
btc_to_swap - bitcoin::Amount::from_sat(bitcoin::TX_FEE)
|
|
|
|
);
|
|
|
|
assert!(btc_bob_final <= bob_btc_starting_balance - btc_to_swap);
|
|
|
|
|
|
|
|
let xmr_alice_final = alice_xmr_wallet.as_ref().get_balance().await.unwrap();
|
|
|
|
bob_xmr_wallet_clone.as_ref().0.refresh().await.unwrap();
|
|
|
|
let xmr_bob_final = bob_xmr_wallet_clone.as_ref().get_balance().await.unwrap();
|
|
|
|
|
|
|
|
assert!(xmr_alice_final <= alice_xmr_starting_balance - xmr_to_swap);
|
|
|
|
assert_eq!(xmr_bob_final, xmr_to_swap);
|
2020-12-10 03:59:09 +00:00
|
|
|
}
|