2020-12-09 22:59:09 -05:00
|
|
|
use rand::rngs::OsRng;
|
2021-01-15 02:34:46 -05:00
|
|
|
use swap::protocol::{alice, alice::AliceState, bob, bob::BobState};
|
2020-12-09 22:59:09 -05:00
|
|
|
|
|
|
|
pub mod testutils;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
2021-01-15 02:34:46 -05:00
|
|
|
testutils::test(|alice_harness, bob, swap_amounts| async move {
|
|
|
|
let alice = alice_harness.new_alice().await;
|
2021-01-14 20:44:15 -05:00
|
|
|
|
|
|
|
let bob_swap_fut = bob::swap(
|
|
|
|
bob.state,
|
|
|
|
bob.event_loop_handle,
|
|
|
|
bob.db,
|
|
|
|
bob.bitcoin_wallet.clone(),
|
|
|
|
bob.monero_wallet.clone(),
|
|
|
|
OsRng,
|
|
|
|
bob.swap_id,
|
|
|
|
);
|
|
|
|
let bob_swap_handle = tokio::spawn(bob_swap_fut);
|
|
|
|
|
|
|
|
let alice_state = alice::run_until(
|
|
|
|
alice.state,
|
|
|
|
alice::swap::is_encsig_learned,
|
|
|
|
alice.event_loop_handle,
|
2021-01-15 02:34:46 -05:00
|
|
|
alice.bitcoin_wallet.clone(),
|
|
|
|
alice.monero_wallet.clone(),
|
2021-01-14 20:44:15 -05:00
|
|
|
alice.config,
|
|
|
|
alice.swap_id,
|
|
|
|
alice.db,
|
2020-12-09 22:59:09 -05:00
|
|
|
)
|
2021-01-14 20:44:15 -05:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(alice_state, AliceState::EncSigLearned {..}));
|
|
|
|
|
2021-01-15 02:34:46 -05:00
|
|
|
let alice = alice_harness.recover_alice_from_db().await;
|
2021-01-14 20:44:15 -05:00
|
|
|
assert!(matches!(alice.state, AliceState::EncSigLearned {..}));
|
|
|
|
|
|
|
|
let alice_state = alice::swap(
|
|
|
|
alice.state,
|
|
|
|
alice.event_loop_handle,
|
2021-01-15 02:34:46 -05:00
|
|
|
alice.bitcoin_wallet.clone(),
|
|
|
|
alice.monero_wallet.clone(),
|
2021-01-14 20:44:15 -05:00
|
|
|
alice.config,
|
|
|
|
alice.swap_id,
|
|
|
|
alice.db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-12-14 01:24:23 -05:00
|
|
|
|
2021-01-15 02:34:46 -05:00
|
|
|
alice_harness.assert_redeemed(alice_state).await;
|
2020-12-09 22:59:09 -05:00
|
|
|
|
2021-01-15 02:34:46 -05:00
|
|
|
let bob_state = bob_swap_handle.await.unwrap();
|
2021-01-14 20:44:15 -05:00
|
|
|
let btc_bob_final = bob.bitcoin_wallet.as_ref().balance().await.unwrap();
|
|
|
|
bob.monero_wallet.as_ref().inner.refresh().await.unwrap();
|
|
|
|
let xmr_bob_final = bob.monero_wallet.as_ref().get_balance().await.unwrap();
|
|
|
|
assert!(matches!(bob_state.unwrap(), BobState::XmrRedeemed));
|
|
|
|
assert!(btc_bob_final <= bob.btc_starting_balance - swap_amounts.btc);
|
|
|
|
assert_eq!(xmr_bob_final, bob.xmr_starting_balance + swap_amounts.xmr);
|
|
|
|
})
|
|
|
|
.await;
|
2020-12-09 22:59:09 -05:00
|
|
|
}
|