From 33a9057b1fdc94052f4c176d359449c4975a53eb Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 21 Jan 2021 19:37:52 +1100 Subject: [PATCH] Move run_until is_target_state comparison functions into testutils --- swap/src/protocol/alice/swap.rs | 14 ------- swap/src/protocol/bob/swap.rs | 16 -------- swap/tests/happy_path_restart_alice.rs | 3 +- .../happy_path_restart_bob_after_comm.rs | 5 +-- ...h_restart_bob_after_lock_proof_received.rs | 8 ++-- .../happy_path_restart_bob_before_comm.rs | 6 +-- swap/tests/punish.rs | 6 +-- swap/tests/refund_restart_alice.rs | 5 +-- swap/tests/refund_restart_alice_cancelled.rs | 7 ++-- swap/tests/testutils/mod.rs | 38 +++++++++++++++++++ 10 files changed, 55 insertions(+), 53 deletions(-) diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index e753a54d..635a75e6 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -49,20 +49,6 @@ pub fn is_complete(state: &AliceState) -> bool { ) } -pub fn is_xmr_locked(state: &AliceState) -> bool { - matches!( - state, - AliceState::XmrLocked{..} - ) -} - -pub fn is_encsig_learned(state: &AliceState) -> bool { - matches!( - state, - AliceState::EncSigLearned{..} - ) -} - pub async fn run(swap: alice::Swap) -> Result { run_until(swap, is_complete).await } diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index 85dcd2be..6b522470 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -24,22 +24,6 @@ pub fn is_complete(state: &BobState) -> bool { ) } -pub fn is_btc_locked(state: &BobState) -> bool { - matches!(state, BobState::BtcLocked(..)) -} - -pub fn is_lock_proof_received(state: &BobState) -> bool { - matches!(state, BobState::XmrLockProofReceived { .. }) -} - -pub fn is_xmr_locked(state: &BobState) -> bool { - matches!(state, BobState::XmrLocked(..)) -} - -pub fn is_encsig_sent(state: &BobState) -> bool { - matches!(state, BobState::EncSigSent(..)) -} - #[allow(clippy::too_many_arguments)] pub async fn run(swap: bob::Swap) -> Result { run_until(swap, is_complete).await diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index f557db8b..afc918d0 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -1,6 +1,7 @@ pub mod testutils; use swap::protocol::{alice, alice::AliceState, bob}; +use testutils::alice_run_until::is_encsig_learned; #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { @@ -11,7 +12,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let bob = bob::run(bob_swap); let bob_handle = tokio::spawn(bob); - let alice_state = alice::run_until(alice_swap, alice::swap::is_encsig_learned) + let alice_state = alice::run_until(alice_swap, is_encsig_learned) .await .unwrap(); assert!(matches!(alice_state, AliceState::EncSigLearned {..})); diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index ca6c62cc..fa1c14ff 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -1,6 +1,7 @@ pub mod testutils; use swap::protocol::{alice, bob, bob::BobState}; +use testutils::bob_run_until::is_encsig_sent; #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { @@ -11,9 +12,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { let alice = alice::run(alice_swap); let alice_handle = tokio::spawn(alice); - let bob_state = bob::run_until(bob_swap, bob::swap::is_encsig_sent) - .await - .unwrap(); + let bob_state = bob::run_until(bob_swap, is_encsig_sent).await.unwrap(); assert!(matches!(bob_state, BobState::EncSigSent {..})); diff --git a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs index 6422ccd9..4d5509c2 100644 --- a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs +++ b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs @@ -1,10 +1,8 @@ -use swap::protocol::{ - alice, bob, - bob::{swap::is_lock_proof_received, BobState}, -}; - pub mod testutils; +use swap::protocol::{alice, bob, bob::BobState}; +use testutils::bob_run_until::is_lock_proof_received; + #[tokio::test] async fn given_bob_restarts_after_lock_proof_received_resume_swap() { testutils::setup_test(|mut ctx| async move { diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index ffbbe4af..e3aa8b77 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -1,9 +1,7 @@ pub mod testutils; -use swap::protocol::{ - alice, bob, - bob::{swap::is_xmr_locked, BobState}, -}; +use swap::protocol::{alice, bob, bob::BobState}; +use testutils::bob_run_until::is_xmr_locked; #[tokio::test] async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 4262b4bd..51487c7f 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -1,9 +1,7 @@ pub mod testutils; -use swap::protocol::{ - alice, bob, - bob::{swap::is_btc_locked, BobState}, -}; +use swap::protocol::{alice, bob, bob::BobState}; +use testutils::bob_run_until::is_btc_locked; /// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// the encsig and fail to refund or redeem. Alice punishes. diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index 031efe48..1b147863 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -1,6 +1,7 @@ pub mod testutils; use swap::protocol::{alice, alice::AliceState, bob}; +use testutils::alice_run_until::is_xmr_locked; /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// then also refunds. @@ -13,9 +14,7 @@ async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { let bob = bob::run(bob_swap); let bob_handle = tokio::spawn(bob); - let alice_state = alice::run_until(alice_swap, alice::swap::is_xmr_locked) - .await - .unwrap(); + let alice_state = alice::run_until(alice_swap, is_xmr_locked).await.unwrap(); assert!(matches!(alice_state, AliceState::XmrLocked {..})); // Alice does not act, Bob refunds diff --git a/swap/tests/refund_restart_alice_cancelled.rs b/swap/tests/refund_restart_alice_cancelled.rs index 5165aaed..24f5415f 100644 --- a/swap/tests/refund_restart_alice_cancelled.rs +++ b/swap/tests/refund_restart_alice_cancelled.rs @@ -1,7 +1,8 @@ -use swap::protocol::{alice, alice::AliceState, bob}; - pub mod testutils; +use swap::protocol::{alice, alice::AliceState, bob}; +use testutils::alice_run_until::is_encsig_learned; + /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// is forced to refund even though she learned the secret and would be able to /// redeem had the timelock not expired. @@ -14,7 +15,7 @@ async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_re let bob = bob::run(bob_swap); let bob_handle = tokio::spawn(bob); - let alice_state = alice::run_until(alice_swap, alice::swap::is_encsig_learned) + let alice_state = alice::run_until(alice_swap, is_encsig_learned) .await .unwrap(); assert!(matches!(alice_state, AliceState::EncSigLearned {..})); diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index f26a1fa7..04b0a144 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -460,3 +460,41 @@ fn init_tracing() -> DefaultGuard { )) .set_default() } + +pub mod alice_run_until { + use swap::protocol::alice::AliceState; + + pub fn is_xmr_locked(state: &AliceState) -> bool { + matches!( + state, + AliceState::XmrLocked{..} + ) + } + + pub fn is_encsig_learned(state: &AliceState) -> bool { + matches!( + state, + AliceState::EncSigLearned{..} + ) + } +} + +pub mod bob_run_until { + use swap::protocol::bob::BobState; + + pub fn is_btc_locked(state: &BobState) -> bool { + matches!(state, BobState::BtcLocked(..)) + } + + pub fn is_lock_proof_received(state: &BobState) -> bool { + matches!(state, BobState::XmrLockProofReceived { .. }) + } + + pub fn is_xmr_locked(state: &BobState) -> bool { + matches!(state, BobState::XmrLocked(..)) + } + + pub fn is_encsig_sent(state: &BobState) -> bool { + matches!(state, BobState::EncSigSent(..)) + } +}