mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Add test where Alice restart after encsign is learned
This commit is contained in:
parent
a22efaa7bc
commit
3580b7ed2c
@ -3,7 +3,7 @@ use futures::future::try_join;
|
|||||||
use libp2p::Multiaddr;
|
use libp2p::Multiaddr;
|
||||||
use monero_harness::{image, Monero};
|
use monero_harness::{image, Monero};
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use std::sync::Arc;
|
use std::{convert::TryInto, sync::Arc};
|
||||||
use swap::{
|
use swap::{
|
||||||
alice, alice::swap::AliceState, bitcoin, bob, bob::swap::BobState, monero,
|
alice, alice::swap::AliceState, bitcoin, bob, bob::swap::BobState, monero,
|
||||||
network::transport::build, storage::Database, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
|
network::transport::build, storage::Database, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK,
|
||||||
@ -80,7 +80,7 @@ async fn setup_wallets(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn alice_safe_restart_after_btc_is_locked() {
|
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
|
||||||
setup_tracing();
|
setup_tracing();
|
||||||
|
|
||||||
let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876"
|
let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9876"
|
||||||
@ -116,9 +116,10 @@ async fn alice_safe_restart_after_btc_is_locked() {
|
|||||||
xmr: xmr_to_swap,
|
xmr: xmr_to_swap,
|
||||||
};
|
};
|
||||||
|
|
||||||
let alice_swap = {
|
let alice_db_dir = TempDir::new().unwrap();
|
||||||
|
let alice_swap_fut = async {
|
||||||
let rng = &mut OsRng;
|
let rng = &mut OsRng;
|
||||||
let (alice_state, alice_behaviour) = {
|
let (alice_start_state, state0) = {
|
||||||
let a = bitcoin::SecretKey::new_random(rng);
|
let a = bitcoin::SecretKey::new_random(rng);
|
||||||
let s_a = cross_curve_dleq::Scalar::random(rng);
|
let s_a = cross_curve_dleq::Scalar::random(rng);
|
||||||
let v_a = xmr_btc::monero::PrivateViewKey::new_random(rng);
|
let v_a = xmr_btc::monero::PrivateViewKey::new_random(rng);
|
||||||
@ -141,26 +142,29 @@ async fn alice_safe_restart_after_btc_is_locked() {
|
|||||||
amounts,
|
amounts,
|
||||||
state0: state0.clone(),
|
state0: state0.clone(),
|
||||||
},
|
},
|
||||||
alice::Behaviour::new(state0),
|
state0,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
let alice_behaviour = alice::Behaviour::new(state0.clone());
|
||||||
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
||||||
let (mut alice_event_loop, alice_event_loop_handle) = alice::event_loop::EventLoop::new(
|
let (mut alice_event_loop_1, alice_event_loop_handle) = alice::event_loop::EventLoop::new(
|
||||||
alice_transport,
|
alice_transport,
|
||||||
alice_behaviour,
|
alice_behaviour,
|
||||||
alice_multiaddr.clone(),
|
alice_multiaddr.clone(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let _alice_swarm_fut = tokio::spawn(async move { alice_event_loop.run().await });
|
let _alice_event_loop_1 = tokio::spawn(async move { alice_event_loop_1.run().await });
|
||||||
|
|
||||||
let config = xmr_btc::config::Config::regtest();
|
let config = xmr_btc::config::Config::regtest();
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
let tmp_dir = TempDir::new().unwrap();
|
|
||||||
let db = Database::open(tmp_dir.path()).unwrap();
|
|
||||||
|
|
||||||
alice::swap::swap(
|
let db = Database::open(alice_db_dir.path()).unwrap();
|
||||||
alice_state,
|
|
||||||
|
// Alice reaches encsig_learned
|
||||||
|
alice::swap::run_until(
|
||||||
|
alice_start_state,
|
||||||
|
|state| matches!(state, AliceState::EncSignLearned { .. }),
|
||||||
alice_event_loop_handle,
|
alice_event_loop_handle,
|
||||||
alice_btc_wallet.clone(),
|
alice_btc_wallet.clone(),
|
||||||
alice_xmr_wallet.clone(),
|
alice_xmr_wallet.clone(),
|
||||||
@ -168,6 +172,37 @@ async fn alice_safe_restart_after_btc_is_locked() {
|
|||||||
swap_id,
|
swap_id,
|
||||||
db,
|
db,
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let db = Database::open(alice_db_dir.path()).unwrap();
|
||||||
|
|
||||||
|
let alice_behaviour = alice::Behaviour::new(state0);
|
||||||
|
let alice_transport = build(alice_behaviour.identity()).unwrap();
|
||||||
|
let (mut alice_event_loop_2, alice_event_loop_handle) = alice::event_loop::EventLoop::new(
|
||||||
|
alice_transport,
|
||||||
|
alice_behaviour,
|
||||||
|
alice_multiaddr.clone(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let _alice_event_loop_2 = tokio::spawn(async move { alice_event_loop_2.run().await });
|
||||||
|
|
||||||
|
// Load the latest state from the db
|
||||||
|
let latest_state = db.get_state(swap_id).unwrap();
|
||||||
|
let latest_state = latest_state.try_into().unwrap();
|
||||||
|
|
||||||
|
// Finish the swap
|
||||||
|
alice::swap::swap(
|
||||||
|
latest_state,
|
||||||
|
alice_event_loop_handle,
|
||||||
|
alice_btc_wallet.clone(),
|
||||||
|
alice_xmr_wallet.clone(),
|
||||||
|
config,
|
||||||
|
swap_id,
|
||||||
|
db,
|
||||||
|
)
|
||||||
|
.await
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bob_swap, bob_event_loop) = {
|
let (bob_swap, bob_event_loop) = {
|
||||||
@ -189,7 +224,7 @@ async fn alice_safe_restart_after_btc_is_locked() {
|
|||||||
let bob_state = BobState::Started {
|
let bob_state = BobState::Started {
|
||||||
state0,
|
state0,
|
||||||
amounts,
|
amounts,
|
||||||
addr: alice_multiaddr,
|
addr: alice_multiaddr.clone(),
|
||||||
};
|
};
|
||||||
let (bob_event_loop, bob_event_loop_handle) =
|
let (bob_event_loop, bob_event_loop_handle) =
|
||||||
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour).unwrap();
|
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour).unwrap();
|
||||||
@ -210,7 +245,7 @@ async fn alice_safe_restart_after_btc_is_locked() {
|
|||||||
|
|
||||||
let _bob_event_loop = tokio::spawn(async move { bob_event_loop.run().await });
|
let _bob_event_loop = tokio::spawn(async move { bob_event_loop.run().await });
|
||||||
|
|
||||||
try_join(alice_swap, bob_swap).await.unwrap();
|
try_join(alice_swap_fut, bob_swap).await.unwrap();
|
||||||
|
|
||||||
let btc_alice_final = alice_btc_wallet.balance().await.unwrap();
|
let btc_alice_final = alice_btc_wallet.balance().await.unwrap();
|
||||||
let xmr_alice_final = alice_xmr_wallet.get_balance().await.unwrap();
|
let xmr_alice_final = alice_xmr_wallet.get_balance().await.unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user