diff --git a/swap/src/main.rs b/swap/src/main.rs index 41c0b6c4..a948bc48 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -302,7 +302,7 @@ async fn alice_swap( let (mut event_loop, handle) = alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen_addr)?; - let swap = alice::swap::swap( + let swap = alice::swap( state, handle, bitcoin_wallet.clone(), @@ -333,7 +333,7 @@ async fn bob_swap( let (event_loop, handle) = bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)?; - let swap = bob::swap::swap( + let swap = bob::swap( state, handle, db, diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 439033ff..286dc8ed 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -18,7 +18,9 @@ use crate::{ protocol::{bob, SwapAmounts}, }; -pub use self::{amounts::*, message0::Message0, message1::Message1, message2::Message2, state::*}; +pub use self::{ + amounts::*, message0::Message0, message1::Message1, message2::Message2, state::*, swap::*, +}; mod amounts; pub mod event_loop; diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 2098f7d8..c427afb6 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -86,6 +86,30 @@ impl fmt::Display for AliceState { } } +pub fn is_complete(state: &AliceState) -> bool { + matches!( + state, + AliceState::XmrRefunded + | AliceState::BtcRedeemed + | AliceState::BtcPunished + | AliceState::SafelyAborted + ) +} + +pub fn is_xmr_locked(state: &AliceState) -> bool { + matches!( + state, + AliceState::XmrLocked{..} + ) +} + +pub fn is_encsig_learned(state: &AliceState) -> bool { + matches!( + state, + AliceState::EncSigLearned{..} + ) +} + #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct State0 { pub a: bitcoin::SecretKey, diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index a9ec7a1e..a5f6c051 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -21,6 +21,7 @@ use crate::{ protocol::{ alice::{ event_loop::EventLoopHandle, + is_complete, steps::{ build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction, extract_monero_private_key, lock_xmr, negotiate, @@ -60,30 +61,6 @@ pub async fn swap( .await } -pub fn is_complete(state: &AliceState) -> bool { - matches!( - state, - AliceState::XmrRefunded - | AliceState::BtcRedeemed - | AliceState::BtcPunished - | AliceState::SafelyAborted - ) -} - -pub fn is_xmr_locked(state: &AliceState) -> bool { - matches!( - state, - AliceState::XmrLocked{..} - ) -} - -pub fn is_encsig_learned(state: &AliceState) -> bool { - matches!( - state, - AliceState::EncSigLearned{..} - ) -} - // State machine driver for swap execution #[async_recursion] #[allow(clippy::too_many_arguments)] diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index 01a922de..3d396a82 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -19,7 +19,7 @@ use crate::{ pub use self::{ amounts::*, message0::Message0, message1::Message1, message2::Message2, message3::Message3, - state::*, + state::*, swap::*, }; mod amounts; diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index d56c8f87..3d300d21 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -58,6 +58,28 @@ impl fmt::Display for BobState { } } +pub fn is_complete(state: &BobState) -> bool { + matches!( + state, + BobState::BtcRefunded(..) + | BobState::XmrRedeemed + | BobState::BtcPunished + | BobState::SafelyAborted + ) +} + +pub fn is_btc_locked(state: &BobState) -> bool { + matches!(state, BobState::BtcLocked(..)) +} + +pub fn is_xmr_locked(state: &BobState) -> bool { + matches!(state, BobState::XmrLocked(..)) +} + +pub fn is_encsig_sent(state: &BobState) -> bool { + matches!(state, BobState::EncSigSent(..)) +} + #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] pub struct State0 { b: bitcoin::SecretKey, diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 1c84f4fe..5093dea7 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -41,28 +41,6 @@ where .await } -pub fn is_complete(state: &BobState) -> bool { - matches!( - state, - BobState::BtcRefunded(..) - | BobState::XmrRedeemed - | BobState::BtcPunished - | BobState::SafelyAborted - ) -} - -pub fn is_btc_locked(state: &BobState) -> bool { - matches!(state, BobState::BtcLocked(..)) -} - -pub fn is_xmr_locked(state: &BobState) -> bool { - matches!(state, BobState::XmrLocked(..)) -} - -pub fn is_encsig_sent(state: &BobState) -> bool { - matches!(state, BobState::EncSigSent(..)) -} - // State machine driver for swap execution #[allow(clippy::too_many_arguments)] #[async_recursion] diff --git a/swap/src/tests/happy_path.rs b/swap/src/tests/happy_path.rs index cbab0257..45505e9c 100644 --- a/swap/src/tests/happy_path.rs +++ b/swap/src/tests/happy_path.rs @@ -80,7 +80,7 @@ async fn happy_path() { ) .await; - let alice_swap_fut = alice::swap::swap( + let alice_swap_fut = alice::swap( alice_state, alice_event_loop_handle, alice_btc_wallet.clone(), @@ -93,7 +93,7 @@ async fn happy_path() { let alice_fut = select(alice_swap_fut, alice_event_loop.run().boxed()); - let bob_swap_fut = bob::swap::swap( + let bob_swap_fut = bob::swap( bob_state, bob_event_loop_handle, bob_db, diff --git a/swap/src/tests/happy_path_restart_alice.rs b/swap/src/tests/happy_path_restart_alice.rs index 7701ef12..5d451f45 100644 --- a/swap/src/tests/happy_path_restart_alice.rs +++ b/swap/src/tests/happy_path_restart_alice.rs @@ -102,9 +102,9 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let alice_swap_id = Uuid::new_v4(); - let alice_state = alice::swap::run_until( + let alice_state = alice::run_until( start_state, - alice::swap::is_encsig_learned, + alice::is_encsig_learned, alice_event_loop_handle, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), diff --git a/swap/src/tests/happy_path_restart_bob_after_comm.rs b/swap/src/tests/happy_path_restart_bob_after_comm.rs index 3a50aea9..e54ee242 100644 --- a/swap/src/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/src/tests/happy_path_restart_bob_after_comm.rs @@ -85,7 +85,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { let bob_btc_wallet_clone = bob_btc_wallet.clone(); let bob_xmr_wallet_clone = bob_xmr_wallet.clone(); - let alice_swap_handle = tokio::spawn(alice::swap::swap( + let alice_swap_handle = tokio::spawn(alice::swap( alice_state, alice_event_loop_handle, alice_btc_wallet.clone(), @@ -103,9 +103,9 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { let bob_db_datadir = tempdir().unwrap(); let bob_db = Database::open(bob_db_datadir.path()).unwrap(); - let bob_state = bob::swap::run_until( + let bob_state = bob::run_until( bob_state, - bob::swap::is_encsig_sent, + bob::is_encsig_sent, bob_event_loop_handle, bob_db, bob_btc_wallet.clone(), @@ -131,7 +131,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { init_bob_event_loop(alice_peer_id, alice_multiaddr); tokio::spawn(event_loop_after_restart.run()); - let bob_state = bob::swap::swap( + let bob_state = bob::swap( resume_state, event_loop_handle_after_restart, bob_db, diff --git a/swap/src/tests/happy_path_restart_bob_before_comm.rs b/swap/src/tests/happy_path_restart_bob_before_comm.rs index 9fb26a4e..0f896070 100644 --- a/swap/src/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/src/tests/happy_path_restart_bob_before_comm.rs @@ -76,7 +76,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { ) .await; - let alice_fut = alice::swap::swap( + let alice_fut = alice::swap( alice_state, alice_event_loop_handle, alice_btc_wallet.clone(), @@ -91,9 +91,9 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { let bob_xmr_locked_fut = { let bob_db = Database::open(bob_db_datadir.path()).unwrap(); - bob::swap::run_until( + bob::run_until( bob_state, - bob::swap::is_xmr_locked, + bob::is_xmr_locked, bob_event_loop_handle_1, bob_db, bob_btc_wallet.clone(), @@ -117,7 +117,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { let (bob_event_loop_2, bob_event_loop_handle_2) = init_bob_event_loop(alice_peer_id, alice_multiaddr); - let bob_fut = bob::swap::swap( + let bob_fut = bob::swap( bob_restart_state, bob_event_loop_handle_2, Database::open(bob_db_datadir.path()).unwrap(), diff --git a/swap/src/tests/punish.rs b/swap/src/tests/punish.rs index 8830f4cc..755f2827 100644 --- a/swap/src/tests/punish.rs +++ b/swap/src/tests/punish.rs @@ -79,9 +79,9 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { ) .await; - let bob_btc_locked_fut = bob::swap::run_until( + let bob_btc_locked_fut = bob::run_until( bob_state, - bob::swap::is_btc_locked, + bob::is_btc_locked, bob_event_loop_handle, bob_db, bob_btc_wallet.clone(), @@ -93,7 +93,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { let bob_fut = select(bob_btc_locked_fut, bob_event_loop.run().boxed()); - let alice_fut = alice::swap::swap( + let alice_fut = alice::swap( alice_state, alice_event_loop_handle, alice_btc_wallet.clone(), diff --git a/swap/src/tests/refund_restart_alice.rs b/swap/src/tests/refund_restart_alice.rs index 42c5e19a..e1a4154f 100644 --- a/swap/src/tests/refund_restart_alice.rs +++ b/swap/src/tests/refund_restart_alice.rs @@ -81,7 +81,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { ) .await; - let bob_fut = bob::swap::swap( + let bob_fut = bob::swap( bob_state, bob_event_loop_handle, bob_db, @@ -96,9 +96,9 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { let alice_xmr_locked_fut = { let alice_db = Database::open(alice_db_datadir.path()).unwrap(); - alice::swap::run_until( + alice::run_until( alice_state, - alice::swap::is_xmr_locked, + alice::is_xmr_locked, alice_event_loop_handle_1, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), @@ -128,7 +128,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { let alice_final_state = { let alice_db = Database::open(alice_db_datadir.path()).unwrap(); - alice::swap::swap( + alice::swap( alice_restart_state, alice_event_loop_handle_2, alice_btc_wallet.clone(),