Move files from protocol to appropriate module

Some network and application specific code does not belong in the protocol module and was moved.
Eventloop, recovery and the outside behaviour were moved to the respective application module because they are application specific.

The `swap_setup` was moved into the network module because upon change both sides will have to be changed and should thus stay close together.
This commit is contained in:
Daniel Karzel 2021-06-25 13:40:33 +10:00
parent 818147a629
commit c0070f8fa7
No known key found for this signature in database
GPG key ID: 30C3FC2E438ADB6E
46 changed files with 161 additions and 145 deletions

View file

@ -3,10 +3,11 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::bob_run_until::is_btc_locked;
use harness::FastCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};
use swap::{asb, cli};
#[tokio::test]
async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
@ -50,7 +51,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
// Bob manually cancels
bob_join_handle.abort();
let (_, state) =
bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
assert!(matches!(state, BobState::BtcCancelled { .. }));
let (bob_swap, bob_join_handle) = ctx
@ -61,7 +62,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
// Bob manually refunds
bob_join_handle.abort();
let bob_state =
bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
cli::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false).await??;
ctx.assert_bob_refunded(bob_state).await;
@ -74,7 +75,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
AliceState::XmrLockTransactionSent { .. }
));
alice::cancel(
asb::cancel(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.db,
@ -86,7 +87,7 @@ async fn given_alice_and_bob_manually_refund_after_funds_locked_both_refund() {
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;
assert!(matches!(alice_swap.state, AliceState::BtcCancelled { .. }));
let alice_state = alice::refund(
let alice_state = asb::refund(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.monero_wallet,

View file

@ -3,10 +3,11 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::bob_run_until::is_btc_locked;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};
use swap::{asb, cli};
#[tokio::test]
async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors() {
@ -37,12 +38,12 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
));
// Bob tries but fails to manually cancel
let result = bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false)
let result = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false)
.await?
.unwrap_err();
assert!(matches!(
result,
bob::cancel::Error::CancelTimelockNotExpiredYet
cli::cancel::Error::CancelTimelockNotExpiredYet
));
ctx.restart_alice().await;
@ -53,7 +54,7 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
));
// Alice tries but fails manual cancel
let result = alice::cancel(
let result = asb::cancel(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.db,
@ -63,7 +64,7 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
.unwrap_err();
assert!(matches!(
result,
alice::cancel::Error::CancelTimelockNotExpiredYet
asb::cancel::Error::CancelTimelockNotExpiredYet
));
let (bob_swap, bob_join_handle) = ctx
@ -72,10 +73,10 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob tries but fails to manually refund
let result = bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false)
let result = cli::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, false)
.await?
.unwrap_err();
assert!(matches!(result, bob::refund::SwapNotCancelledYet(_)));
assert!(matches!(result, cli::refund::SwapNotCancelledYet(_)));
let (bob_swap, _) = ctx
.stop_and_resume_bob_from_db(bob_join_handle, swap_id)
@ -90,7 +91,7 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
));
// Alice tries but fails manual cancel
let result = alice::refund(
let result = asb::refund(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.monero_wallet,
@ -99,7 +100,7 @@ async fn given_alice_and_bob_manually_cancel_when_timelock_not_expired_errors()
)
.await?
.unwrap_err();
assert!(matches!(result, alice::refund::Error::SwapNotCancelled));
assert!(matches!(result, asb::refund::Error::SwapNotCancelled));
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;

View file

@ -3,10 +3,11 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::bob_run_until::is_btc_locked;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};
use swap::{asb, cli};
#[tokio::test]
async fn given_alice_and_bob_manually_force_cancel_when_timelock_not_expired_errors() {
@ -37,7 +38,7 @@ async fn given_alice_and_bob_manually_force_cancel_when_timelock_not_expired_err
));
// Bob tries but fails to manually cancel
let result = bob::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, true).await;
let result = cli::cancel(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, true).await;
assert!(matches!(result, Err(_)));
ctx.restart_alice().await;
@ -48,7 +49,7 @@ async fn given_alice_and_bob_manually_force_cancel_when_timelock_not_expired_err
));
// Alice tries but fails manual cancel
let is_outer_err = alice::cancel(
let is_outer_err = asb::cancel(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.db,
@ -64,7 +65,7 @@ async fn given_alice_and_bob_manually_force_cancel_when_timelock_not_expired_err
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
// Bob tries but fails to manually refund
let is_outer_err = bob::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, true)
let is_outer_err = cli::refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db, true)
.await
.is_err();
assert!(is_outer_err);
@ -82,7 +83,7 @@ async fn given_alice_and_bob_manually_force_cancel_when_timelock_not_expired_err
));
// Alice tries but fails manual cancel
let refund_tx_not_published_yet = alice::refund(
let refund_tx_not_published_yet = asb::refund(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.monero_wallet,
@ -93,7 +94,7 @@ async fn given_alice_and_bob_manually_force_cancel_when_timelock_not_expired_err
.unwrap_err();
assert!(matches!(
refund_tx_not_published_yet,
alice::refund::Error::RefundTransactionNotPublishedYet(..)
asb::refund::Error::RefundTransactionNotPublishedYet(..)
));
ctx.restart_alice().await;

View file

@ -3,7 +3,8 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::bob_run_until::is_btc_locked;
use harness::FastPunishConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};
@ -47,7 +48,7 @@ async fn alice_manually_punishes_after_bob_dead() {
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;
let (_, alice_state) = alice::cancel(
let (_, alice_state) = asb::cancel(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.db,
@ -70,7 +71,7 @@ async fn alice_manually_punishes_after_bob_dead() {
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;
let (_, alice_state) = alice::punish(
let (_, alice_state) = asb::punish(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.db,

View file

@ -2,8 +2,8 @@ pub mod harness;
use harness::alice_run_until::is_encsig_learned;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::protocol::alice::redeem::Finality;
use swap::asb;
use swap::asb::{Finality, FixedRate};
use swap::protocol::alice::AliceState;
use swap::protocol::{alice, bob};
@ -28,7 +28,7 @@ async fn alice_manually_redeems_after_enc_sig_learned() {
// manual redeem
ctx.restart_alice().await;
let alice_swap = ctx.alice_next_swap().await;
let (_, alice_state) = alice::redeem(
let (_, alice_state) = asb::redeem(
alice_swap.swap_id,
alice_swap.bitcoin_wallet,
alice_swap.db,

View file

@ -3,7 +3,7 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::bob_run_until::is_btc_locked;
use harness::FastPunishConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::FastCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::{alice, bob};

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::bob_run_until::is_xmr_locked;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::bob_run_until::is_btc_locked;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};

View file

@ -1,7 +1,7 @@
pub mod harness;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::{alice, bob};
use tokio::join;

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::alice_run_until::is_xmr_lock_transaction_sent;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::alice::AliceState;
use swap::protocol::{alice, bob};

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::bob_run_until::is_xmr_locked;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::bob_run_until::is_xmr_locked;
use harness::SlowCancelConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};

View file

@ -14,16 +14,16 @@ use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use swap::asb::FixedRate;
use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund};
use swap::database::Database;
use swap::env::{Config, GetConfig};
use swap::network::swarm;
use swap::protocol::alice::event_loop::FixedRate;
use swap::protocol::alice::{AliceState, Swap};
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};
use swap::seed::Seed;
use swap::{bitcoin, env, monero};
use swap::{asb, bitcoin, cli, env, monero};
use tempfile::tempdir;
use testcontainers::clients::Cli;
use testcontainers::{Container, Docker, RunArgs};
@ -240,7 +240,7 @@ async fn start_alice(
.unwrap();
swarm.listen_on(listen_address).unwrap();
let (event_loop, swap_handle) = alice::EventLoop::new(
let (event_loop, swap_handle) = asb::EventLoop::new(
swarm,
env_config,
bitcoin_wallet,
@ -399,7 +399,7 @@ struct BobParams {
}
impl BobParams {
pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, bob::EventLoop)> {
pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, cli::EventLoop)> {
let (event_loop, handle) = self.new_eventloop(swap_id).await?;
let db = Database::open(&self.db_path)?;
@ -419,7 +419,7 @@ impl BobParams {
pub async fn new_swap(
&self,
btc_amount: bitcoin::Amount,
) -> Result<(bob::Swap, bob::EventLoop)> {
) -> Result<(bob::Swap, cli::EventLoop)> {
let swap_id = Uuid::new_v4();
let (event_loop, handle) = self.new_eventloop(swap_id).await?;
@ -442,7 +442,7 @@ impl BobParams {
pub async fn new_eventloop(
&self,
swap_id: Uuid,
) -> Result<(bob::EventLoop, bob::EventLoopHandle)> {
) -> Result<(cli::EventLoop, cli::EventLoopHandle)> {
let tor_socks5_port = get_port()
.expect("We don't care about Tor in the tests so we get a free port to disable it.");
let mut swarm = swarm::cli(
@ -457,7 +457,7 @@ impl BobParams {
.behaviour_mut()
.add_address(self.alice_peer_id, self.alice_address.clone());
bob::EventLoop::new(swap_id, swarm, self.alice_peer_id, self.env_config)
cli::EventLoop::new(swap_id, swarm, self.alice_peer_id, self.env_config)
}
}

View file

@ -2,7 +2,7 @@ pub mod harness;
use harness::bob_run_until::is_btc_locked;
use harness::FastPunishConfig;
use swap::protocol::alice::event_loop::FixedRate;
use swap::asb::FixedRate;
use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob};