diff --git a/swap/tests/happy_path_restart.rs b/swap/tests/happy_path_restart.rs index f34deeef..e57381cc 100644 --- a/swap/tests/happy_path_restart.rs +++ b/swap/tests/happy_path_restart.rs @@ -110,7 +110,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { } let (mut event_loop_after_restart, event_loop_handle_after_restart) = - testutils::init_alice_eventloop(alice_multiaddr); + testutils::init_alice_event_loop(alice_multiaddr); let _alice_swarm_fut = tokio::spawn(async move { event_loop_after_restart.run().await }); let alice_state = alice::swap::recover( diff --git a/swap/tests/refund_restart.rs b/swap/tests/refund_restart.rs index 4c492e77..d44294ff 100644 --- a/swap/tests/refund_restart.rs +++ b/swap/tests/refund_restart.rs @@ -109,7 +109,7 @@ async fn both_refund() { let alice_db = Database::open(alice_db_datadir.path()).unwrap(); let (mut alice_event_loop, alice_event_loop_handle) = - testutils::init_alice_eventloop(alice_multiaddr); + testutils::init_alice_event_loop(alice_multiaddr); let alice_state = alice::swap::swap( alice_state, diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index a352787a..d008c65c 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -13,119 +13,6 @@ use tracing_core::dispatcher::DefaultGuard; use tracing_log::LogTracer; use xmr_btc::{alice::State0, config::Config, cross_curve_dleq}; -#[allow(clippy::too_many_arguments)] -pub async fn init_bob( - alice_multiaddr: Multiaddr, - bitcoind: &Bitcoind<'_>, - monero: &Monero, - btc_to_swap: bitcoin::Amount, - btc_starting_balance: bitcoin::Amount, - xmr_to_swap: xmr_btc::monero::Amount, - config: Config, -) -> ( - BobState, - bob::event_loop::EventLoop, - bob::event_loop::EventLoopHandle, - Arc, - Arc, - Database, -) { - let (bob_btc_wallet, bob_xmr_wallet) = init_wallets( - "bob", - bitcoind, - monero, - Some(btc_starting_balance), - None, - config, - ) - .await; - - let amounts = SwapAmounts { - btc: btc_to_swap, - xmr: xmr_to_swap, - }; - - let bob_db_dir = tempdir().unwrap(); - let bob_db = Database::open(bob_db_dir.path()).unwrap(); - let bob_behaviour = bob::Behaviour::default(); - let bob_transport = build(bob_behaviour.identity()).unwrap(); - - let refund_address = bob_btc_wallet.new_address().await.unwrap(); - let state0 = xmr_btc::bob::State0::new( - &mut OsRng, - btc_to_swap, - xmr_to_swap, - config.bitcoin_refund_timelock, - config.bitcoin_punish_timelock, - refund_address, - ); - let bob_state = BobState::Started { - state0, - amounts, - addr: alice_multiaddr, - }; - - let (swarm_driver, swarm_handle) = - bob::event_loop::EventLoop::new(bob_transport, bob_behaviour).unwrap(); - - ( - bob_state, - swarm_driver, - swarm_handle, - bob_btc_wallet, - bob_xmr_wallet, - bob_db, - ) -} - -pub async fn init_alice_state( - btc_to_swap: bitcoin::Amount, - xmr_to_swap: monero::Amount, - alice_btc_wallet: Arc, - config: Config, -) -> AliceState { - let rng = &mut OsRng; - - let amounts = SwapAmounts { - btc: btc_to_swap, - xmr: xmr_to_swap, - }; - - let a = crate::bitcoin::SecretKey::new_random(rng); - let s_a = cross_curve_dleq::Scalar::random(rng); - let v_a = xmr_btc::monero::PrivateViewKey::new_random(rng); - let redeem_address = alice_btc_wallet.as_ref().new_address().await.unwrap(); - let punish_address = redeem_address.clone(); - let state0 = State0::new( - a, - s_a, - v_a, - amounts.btc, - amounts.xmr, - config.bitcoin_refund_timelock, - config.bitcoin_punish_timelock, - redeem_address, - punish_address, - ); - - AliceState::Started { amounts, state0 } -} - -pub fn init_alice_eventloop( - listen: Multiaddr, -) -> ( - alice::event_loop::EventLoop, - alice::event_loop::EventLoopHandle, -) { - let alice_behaviour = alice::Behaviour::default(); - let alice_transport = build(alice_behaviour.identity()).unwrap(); - - let (swarm_driver, handle) = - alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap(); - - (swarm_driver, handle) -} - pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) { let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); let _ = bitcoind.init(5).await; @@ -177,6 +64,51 @@ pub async fn init_wallets( (btc_wallet, xmr_wallet) } +pub async fn init_alice_state( + btc_to_swap: bitcoin::Amount, + xmr_to_swap: monero::Amount, + alice_btc_wallet: Arc, + config: Config, +) -> AliceState { + let rng = &mut OsRng; + + let amounts = SwapAmounts { + btc: btc_to_swap, + xmr: xmr_to_swap, + }; + + let a = crate::bitcoin::SecretKey::new_random(rng); + let s_a = cross_curve_dleq::Scalar::random(rng); + let v_a = xmr_btc::monero::PrivateViewKey::new_random(rng); + let redeem_address = alice_btc_wallet.as_ref().new_address().await.unwrap(); + let punish_address = redeem_address.clone(); + let state0 = State0::new( + a, + s_a, + v_a, + amounts.btc, + amounts.xmr, + config.bitcoin_refund_timelock, + config.bitcoin_punish_timelock, + redeem_address, + punish_address, + ); + + AliceState::Started { amounts, state0 } +} + +pub fn init_alice_event_loop( + listen: Multiaddr, +) -> ( + alice::event_loop::EventLoop, + alice::event_loop::EventLoopHandle, +) { + let alice_behaviour = alice::Behaviour::default(); + let alice_transport = build(alice_behaviour.identity()).unwrap(); + + alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap() +} + #[allow(clippy::too_many_arguments)] pub async fn init_alice( bitcoind: &Bitcoind<'_>, @@ -206,7 +138,7 @@ pub async fn init_alice( let alice_start_state = init_alice_state(btc_to_swap, xmr_to_swap, alice_btc_wallet.clone(), config).await; - let (swarm_driver, handle) = init_alice_eventloop(listen); + let (swarm_driver, handle) = init_alice_event_loop(listen); ( alice_start_state, @@ -217,6 +149,92 @@ pub async fn init_alice( ) } +pub async fn init_bob_state( + alice_multiaddr: Multiaddr, + btc_to_swap: bitcoin::Amount, + xmr_to_swap: xmr_btc::monero::Amount, + bob_btc_wallet: Arc, + config: Config, +) -> BobState { + let amounts = SwapAmounts { + btc: btc_to_swap, + xmr: xmr_to_swap, + }; + + let refund_address = bob_btc_wallet.new_address().await.unwrap(); + let state0 = xmr_btc::bob::State0::new( + &mut OsRng, + btc_to_swap, + xmr_to_swap, + config.bitcoin_refund_timelock, + config.bitcoin_punish_timelock, + refund_address, + ); + + BobState::Started { + state0, + amounts, + addr: alice_multiaddr, + } +} + +pub fn init_bob_event_loop() -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { + let bob_behaviour = bob::Behaviour::default(); + let bob_transport = build(bob_behaviour.identity()).unwrap(); + bob::event_loop::EventLoop::new(bob_transport, bob_behaviour).unwrap() +} + +#[allow(clippy::too_many_arguments)] +pub async fn init_bob( + alice_multiaddr: Multiaddr, + bitcoind: &Bitcoind<'_>, + monero: &Monero, + btc_to_swap: bitcoin::Amount, + btc_starting_balance: bitcoin::Amount, + xmr_to_swap: xmr_btc::monero::Amount, + config: Config, +) -> ( + BobState, + bob::event_loop::EventLoop, + bob::event_loop::EventLoopHandle, + Arc, + Arc, + Database, +) { + let (bob_btc_wallet, bob_xmr_wallet) = init_wallets( + "bob", + bitcoind, + monero, + Some(btc_starting_balance), + None, + config, + ) + .await; + + let bob_state = init_bob_state( + alice_multiaddr, + btc_to_swap, + xmr_to_swap, + bob_btc_wallet.clone(), + config, + ) + .await; + + let (event_loop, event_loop_handle) = init_bob_event_loop(); + + let bob_db_dir = tempdir().unwrap(); + let bob_db = Database::open(bob_db_dir.path()).unwrap(); + + ( + bob_state, + event_loop, + event_loop_handle, + bob_btc_wallet, + bob_xmr_wallet, + bob_db, + ) +} + // This is just to keep the containers alive #[allow(dead_code)] pub struct Containers<'a> {