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.
pull/586/head
Daniel Karzel 3 years ago
parent 818147a629
commit c0070f8fa7
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E

@ -1,7 +1,18 @@
mod behaviour;
pub mod command;
pub mod config;
mod event_loop;
mod rate;
mod recovery;
pub mod tracing;
pub mod transport;
pub use behaviour::{Behaviour, OutEvent};
pub use event_loop::{EventLoop, EventLoopHandle, FixedRate, KrakenRate, LatestRate};
pub use rate::Rate;
pub use recovery::cancel::cancel;
pub use recovery::punish::punish;
pub use recovery::redeem::{redeem, Finality};
pub use recovery::refund::refund;
pub use recovery::safely_abort::safely_abort;
pub use recovery::{cancel, refund};

@ -1,9 +1,10 @@
use crate::asb::event_loop::LatestRate;
use crate::env;
use crate::network::quote::BidQuote;
use crate::network::swap_setup::alice;
use crate::network::swap_setup::alice::WalletSnapshot;
use crate::network::{encrypted_signature, quote, transfer_proof};
use crate::protocol::alice::event_loop::LatestRate;
use crate::protocol::alice::swap_setup::WalletSnapshot;
use crate::protocol::alice::{swap_setup, State3};
use crate::protocol::alice::State3;
use anyhow::{anyhow, Error};
use libp2p::ping::{Ping, PingEvent};
use libp2p::request_response::{RequestId, ResponseChannel};
@ -22,7 +23,7 @@ pub enum OutEvent {
},
SwapDeclined {
peer: PeerId,
error: swap_setup::Error,
error: alice::Error,
},
QuoteRequested {
channel: ResponseChannel<BidQuote>,
@ -71,7 +72,7 @@ where
LR: LatestRate + Send + 'static,
{
pub quote: quote::Behaviour,
pub swap_setup: swap_setup::Behaviour<LR>,
pub swap_setup: alice::Behaviour<LR>,
pub transfer_proof: transfer_proof::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour,
@ -94,7 +95,7 @@ where
) -> Self {
Self {
quote: quote::alice(),
swap_setup: swap_setup::Behaviour::new(
swap_setup: alice::Behaviour::new(
min_buy,
max_buy,
env_config,

@ -1,10 +1,11 @@
use crate::asb::behaviour::{Behaviour, OutEvent};
use crate::asb::Rate;
use crate::database::Database;
use crate::env::Config;
use crate::network::quote::BidQuote;
use crate::network::swap_setup::alice::WalletSnapshot;
use crate::network::transfer_proof;
use crate::protocol::alice::swap_setup::WalletSnapshot;
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State3, Swap};
use crate::protocol::alice::{AliceState, State3, Swap};
use crate::{bitcoin, kraken, monero};
use anyhow::{Context, Result};
use futures::future;

@ -26,12 +26,11 @@ use swap::asb::command::{parse_args, Arguments, Command};
use swap::asb::config::{
initial_setup, query_user_for_initial_config, read_config, Config, ConfigNotInitialized,
};
use swap::asb::{cancel, punish, redeem, refund, safely_abort, EventLoop, Finality, KrakenRate};
use swap::database::Database;
use swap::monero::Amount;
use swap::network::swarm;
use swap::protocol::alice;
use swap::protocol::alice::event_loop::KrakenRate;
use swap::protocol::alice::{redeem, run, EventLoop};
use swap::protocol::alice::run;
use swap::seed::Seed;
use swap::tor::AuthenticatedClient;
use swap::{asb, bitcoin, kraken, monero, tor};
@ -237,7 +236,7 @@ async fn main() -> Result<()> {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let (txid, _) =
alice::cancel(swap_id, Arc::new(bitcoin_wallet), Arc::new(db), force).await??;
cancel(swap_id, Arc::new(bitcoin_wallet), Arc::new(db), force).await??;
tracing::info!("Cancel transaction successfully published with id {}", txid);
}
@ -245,7 +244,7 @@ async fn main() -> Result<()> {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let monero_wallet = init_monero_wallet(&config, env_config).await?;
alice::refund(
refund(
swap_id,
Arc::new(bitcoin_wallet),
Arc::new(monero_wallet),
@ -260,12 +259,12 @@ async fn main() -> Result<()> {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let (txid, _) =
alice::punish(swap_id, Arc::new(bitcoin_wallet), Arc::new(db), force).await??;
punish(swap_id, Arc::new(bitcoin_wallet), Arc::new(db), force).await??;
tracing::info!("Punish transaction successfully published with id {}", txid);
}
Command::SafelyAbort { swap_id } => {
alice::safely_abort(swap_id, Arc::new(db)).await?;
safely_abort(swap_id, Arc::new(db)).await?;
tracing::info!("Swap safely aborted");
}
@ -276,12 +275,12 @@ async fn main() -> Result<()> {
} => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let (txid, _) = alice::redeem(
let (txid, _) = redeem(
swap_id,
Arc::new(bitcoin_wallet),
Arc::new(db),
force,
redeem::Finality::from_bool(do_not_await_finality),
Finality::from_bool(do_not_await_finality),
)
.await?;

@ -24,12 +24,13 @@ use std::sync::Arc;
use std::time::Duration;
use swap::bitcoin::TxLock;
use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command, ParseResult};
use swap::cli::EventLoop;
use swap::database::Database;
use swap::env::Config;
use swap::network::quote::BidQuote;
use swap::network::swarm;
use swap::protocol::bob;
use swap::protocol::bob::{EventLoop, Swap};
use swap::protocol::bob::Swap;
use swap::seed::Seed;
use swap::{bitcoin, cli, monero};
use tracing::{debug, error, info, warn};
@ -245,13 +246,13 @@ async fn main() -> Result<()> {
)
.await?;
let cancel = bob::cancel(swap_id, Arc::new(bitcoin_wallet), db, force).await?;
let cancel = cli::cancel(swap_id, Arc::new(bitcoin_wallet), db, force).await?;
match cancel {
Ok((txid, _)) => {
debug!("Cancel transaction successfully published with id {}", txid)
}
Err(bob::cancel::Error::CancelTimelockNotExpiredYet) => error!(
Err(cli::cancel::Error::CancelTimelockNotExpiredYet) => error!(
"The Cancel Transaction cannot be published yet, because the timelock has not expired. Please try again later"
),
}
@ -277,7 +278,7 @@ async fn main() -> Result<()> {
)
.await?;
bob::refund(swap_id, Arc::new(bitcoin_wallet), db, force).await??;
cli::refund(swap_id, Arc::new(bitcoin_wallet), db, force).await??;
}
};
Ok(())
@ -428,12 +429,15 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::determine_btc_to_swap;
use ::bitcoin::Amount;
use std::sync::Mutex;
use ::bitcoin::Amount;
use tracing::subscriber;
use crate::determine_btc_to_swap;
use super::*;
struct MaxGiveable {
amounts: Vec<Amount>,
call_counter: usize,

@ -1,3 +1,12 @@
mod behaviour;
pub mod cancel;
pub mod command;
mod event_loop;
pub mod refund;
pub mod tracing;
pub mod transport;
pub use behaviour::{Behaviour, OutEvent};
pub use cancel::cancel;
pub use event_loop::{EventLoop, EventLoopHandle};
pub use refund::refund;

@ -1,6 +1,7 @@
use crate::network::quote::BidQuote;
use crate::network::swap_setup::bob;
use crate::network::{encrypted_signature, quote, redial, transfer_proof};
use crate::protocol::bob::{swap_setup, State2};
use crate::protocol::bob::State2;
use crate::{bitcoin, env};
use anyhow::{anyhow, Error, Result};
use libp2p::core::Multiaddr;
@ -59,7 +60,7 @@ impl OutEvent {
#[allow(missing_debug_implementations)]
pub struct Behaviour {
pub quote: quote::Behaviour,
pub swap_setup: swap_setup::Behaviour,
pub swap_setup: bob::Behaviour,
pub transfer_proof: transfer_proof::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour,
pub redial: redial::Behaviour,
@ -78,7 +79,7 @@ impl Behaviour {
) -> Self {
Self {
quote: quote::bob(),
swap_setup: swap_setup::Behaviour::new(env_config, bitcoin_wallet),
swap_setup: bob::Behaviour::new(env_config, bitcoin_wallet),
transfer_proof: transfer_proof::bob(),
encrypted_signature: encrypted_signature::bob(),
redial: redial::Behaviour::new(alice, Duration::from_secs(2)),

@ -1,8 +1,9 @@
use crate::bitcoin::EncryptedSignature;
use crate::cli::behaviour::{Behaviour, OutEvent};
use crate::network::encrypted_signature;
use crate::network::quote::BidQuote;
use crate::protocol::bob::swap_setup::NewSwap;
use crate::protocol::bob::{Behaviour, OutEvent, State2};
use crate::network::swap_setup::bob::NewSwap;
use crate::protocol::bob::State2;
use crate::{env, monero};
use anyhow::{Context, Result};
use futures::future::{BoxFuture, OptionFuture};

@ -1,5 +1,7 @@
mod impl_from_rr_event;
pub mod alice;
pub mod bob;
pub mod cbor_request_response;
pub mod encrypted_signature;
pub mod json_pull_codec;

@ -1,5 +1,5 @@
use crate::network::cbor_request_response::CborCodec;
use crate::protocol::{alice, bob};
use crate::{asb, cli};
use libp2p::core::ProtocolName;
use libp2p::request_response::{
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
@ -46,7 +46,7 @@ pub fn bob() -> Behaviour {
)
}
impl From<(PeerId, Message)> for alice::OutEvent {
impl From<(PeerId, Message)> for asb::OutEvent {
fn from((peer, message): (PeerId, Message)) -> Self {
match message {
Message::Request {
@ -60,9 +60,9 @@ impl From<(PeerId, Message)> for alice::OutEvent {
}
}
}
crate::impl_from_rr_event!(OutEvent, alice::OutEvent, PROTOCOL);
crate::impl_from_rr_event!(OutEvent, asb::OutEvent, PROTOCOL);
impl From<(PeerId, Message)> for bob::OutEvent {
impl From<(PeerId, Message)> for cli::OutEvent {
fn from((peer, message): (PeerId, Message)) -> Self {
match message {
Message::Request { .. } => Self::unexpected_request(peer),
@ -72,4 +72,4 @@ impl From<(PeerId, Message)> for bob::OutEvent {
}
}
}
crate::impl_from_rr_event!(OutEvent, bob::OutEvent, PROTOCOL);
crate::impl_from_rr_event!(OutEvent, cli::OutEvent, PROTOCOL);

@ -1,6 +1,5 @@
use crate::bitcoin;
use crate::network::json_pull_codec::JsonPullCodec;
use crate::protocol::{alice, bob};
use crate::{asb, bitcoin, cli};
use libp2p::core::ProtocolName;
use libp2p::request_response::{
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
@ -60,7 +59,7 @@ pub fn bob() -> Behaviour {
)
}
impl From<(PeerId, Message)> for alice::OutEvent {
impl From<(PeerId, Message)> for asb::OutEvent {
fn from((peer, message): (PeerId, Message)) -> Self {
match message {
Message::Request { channel, .. } => Self::QuoteRequested { channel, peer },
@ -68,9 +67,9 @@ impl From<(PeerId, Message)> for alice::OutEvent {
}
}
}
crate::impl_from_rr_event!(OutEvent, alice::OutEvent, PROTOCOL);
crate::impl_from_rr_event!(OutEvent, asb::OutEvent, PROTOCOL);
impl From<(PeerId, Message)> for bob::OutEvent {
impl From<(PeerId, Message)> for cli::OutEvent {
fn from((peer, message): (PeerId, Message)) -> Self {
match message {
Message::Request { .. } => Self::unexpected_request(peer),
@ -84,4 +83,4 @@ impl From<(PeerId, Message)> for bob::OutEvent {
}
}
}
crate::impl_from_rr_event!(OutEvent, bob::OutEvent, PROTOCOL);
crate::impl_from_rr_event!(OutEvent, cli::OutEvent, PROTOCOL);

@ -1,4 +1,4 @@
use crate::protocol::bob;
use crate::cli;
use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
use futures::future::FutureExt;
@ -119,11 +119,11 @@ impl NetworkBehaviour for Behaviour {
}
}
impl From<OutEvent> for bob::OutEvent {
impl From<OutEvent> for cli::OutEvent {
fn from(event: OutEvent) -> Self {
match event {
OutEvent::AllAttemptsExhausted { peer } => {
bob::OutEvent::AllRedialAttemptsExhausted { peer }
cli::OutEvent::AllRedialAttemptsExhausted { peer }
}
}
}

@ -5,6 +5,9 @@ use libp2p::swarm::NegotiatedSubstream;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
pub mod alice;
pub mod bob;
pub const BUF_SIZE: usize = 1024 * 1024;
pub mod protocol {

@ -1,11 +1,11 @@
use crate::asb::LatestRate;
use crate::network::swap_setup;
use crate::network::swap_setup::{
protocol, BlockchainNetwork, SpotPriceError, SpotPriceRequest, SpotPriceResponse,
};
use crate::protocol::alice::event_loop::LatestRate;
use crate::protocol::alice::{State0, State3};
use crate::protocol::{alice, Message0, Message2, Message4};
use crate::{bitcoin, env, monero};
use crate::protocol::{Message0, Message2, Message4};
use crate::{asb, bitcoin, env, monero};
use anyhow::{anyhow, Context, Result};
use futures::future::{BoxFuture, OptionFuture};
use futures::FutureExt;
@ -81,24 +81,24 @@ impl WalletSnapshot {
}
}
impl From<OutEvent> for alice::OutEvent {
impl From<OutEvent> for asb::OutEvent {
fn from(event: OutEvent) -> Self {
match event {
OutEvent::Initiated {
send_wallet_snapshot,
} => alice::OutEvent::SwapSetupInitiated {
} => asb::OutEvent::SwapSetupInitiated {
send_wallet_snapshot,
},
OutEvent::Completed {
peer_id: bob_peer_id,
swap_id,
state3,
} => alice::OutEvent::SwapSetupCompleted {
} => asb::OutEvent::SwapSetupCompleted {
peer_id: bob_peer_id,
swap_id,
state3: Box::new(state3),
},
OutEvent::Error { peer_id, error } => alice::OutEvent::Failure {
OutEvent::Error { peer_id, error } => asb::OutEvent::Failure {
peer: peer_id,
error: anyhow!(error),
},
@ -194,7 +194,7 @@ where
}
}
type InboundStream = BoxFuture<'static, Result<(Uuid, alice::State3)>>;
type InboundStream = BoxFuture<'static, Result<(Uuid, State3)>>;
pub struct Handler<LR> {
inbound_stream: OptionFuture<InboundStream>,
@ -236,7 +236,7 @@ impl<LR> Handler<LR> {
#[allow(clippy::large_enum_variant)]
pub enum HandlerOutEvent {
Initiated(bmrng::RequestReceiver<bitcoin::Amount, WalletSnapshot>),
Completed(Result<(Uuid, alice::State3)>),
Completed(Result<(Uuid, State3)>),
}
impl<LR> ProtocolsHandler for Handler<LR>

@ -2,9 +2,9 @@ use crate::network::swap_setup::{
protocol, read_cbor_message, write_cbor_message, BlockchainNetwork, SpotPriceError,
SpotPriceRequest, SpotPriceResponse,
};
use crate::protocol::bob::State0;
use crate::protocol::{bob, Message1, Message3};
use crate::{bitcoin, env, monero};
use crate::protocol::bob::{State0, State2};
use crate::protocol::{Message1, Message3};
use crate::{bitcoin, cli, env, monero};
use anyhow::Result;
use futures::future::{BoxFuture, OptionFuture};
use futures::FutureExt;
@ -46,9 +46,9 @@ impl Behaviour {
}
}
impl From<Completed> for bob::OutEvent {
impl From<Completed> for cli::OutEvent {
fn from(completed: Completed) -> Self {
bob::OutEvent::SwapSetupCompleted(Box::new(completed.0))
cli::OutEvent::SwapSetupCompleted(Box::new(completed.0))
}
}
@ -93,7 +93,7 @@ impl NetworkBehaviour for Behaviour {
}
}
type OutboundStream = BoxFuture<'static, Result<bob::State2>>;
type OutboundStream = BoxFuture<'static, Result<State2>>;
pub struct Handler {
outbound_stream: OptionFuture<OutboundStream>,
@ -126,7 +126,7 @@ pub struct NewSwap {
pub bitcoin_refund_address: bitcoin::Address,
}
pub struct Completed(Result<bob::State2>);
pub struct Completed(Result<State2>);
impl ProtocolsHandler for Handler {
type InEvent = NewSwap;

@ -1,5 +1,4 @@
use crate::protocol::alice::event_loop::LatestRate;
use crate::protocol::{alice, bob};
use crate::asb::LatestRate;
use crate::seed::Seed;
use crate::{asb, bitcoin, cli, env, tor};
use anyhow::Result;
@ -16,11 +15,11 @@ pub fn asb<LR>(
latest_rate: LR,
resume_only: bool,
env_config: env::Config,
) -> Result<Swarm<alice::Behaviour<LR>>>
) -> Result<Swarm<asb::Behaviour<LR>>>
where
LR: LatestRate + Send + 'static + Debug + Clone,
{
let behaviour = alice::Behaviour::new(min_buy, max_buy, latest_rate, resume_only, env_config);
let behaviour = asb::Behaviour::new(min_buy, max_buy, latest_rate, resume_only, env_config);
let identity = seed.derive_libp2p_identity();
let transport = asb::transport::new(&identity)?;
@ -41,13 +40,13 @@ pub async fn cli(
tor_socks5_port: u16,
env_config: env::Config,
bitcoin_wallet: Arc<bitcoin::Wallet>,
) -> Result<Swarm<bob::Behaviour>> {
) -> Result<Swarm<cli::Behaviour>> {
let maybe_tor_socks5_port = match tor::Client::new(tor_socks5_port).assert_tor_running().await {
Ok(()) => Some(tor_socks5_port),
Err(_) => None,
};
let behaviour = bob::Behaviour::new(alice, env_config, bitcoin_wallet);
let behaviour = cli::Behaviour::new(alice, env_config, bitcoin_wallet);
let identity = seed.derive_libp2p_identity();
let transport = cli::transport::new(&identity, maybe_tor_socks5_port)?;

@ -1,6 +1,5 @@
use crate::monero;
use crate::network::cbor_request_response::CborCodec;
use crate::protocol::{alice, bob};
use crate::{asb, cli, monero};
use libp2p::core::ProtocolName;
use libp2p::request_response::{
ProtocolSupport, RequestResponse, RequestResponseConfig, RequestResponseEvent,
@ -47,7 +46,7 @@ pub fn bob() -> Behaviour {
)
}
impl From<(PeerId, Message)> for alice::OutEvent {
impl From<(PeerId, Message)> for asb::OutEvent {
fn from((peer, message): (PeerId, Message)) -> Self {
match message {
Message::Request { .. } => Self::unexpected_request(peer),
@ -58,9 +57,9 @@ impl From<(PeerId, Message)> for alice::OutEvent {
}
}
}
crate::impl_from_rr_event!(OutEvent, alice::OutEvent, PROTOCOL);
crate::impl_from_rr_event!(OutEvent, asb::OutEvent, PROTOCOL);
impl From<(PeerId, Message)> for bob::OutEvent {
impl From<(PeerId, Message)> for cli::OutEvent {
fn from((peer, message): (PeerId, Message)) -> Self {
match message {
Message::Request {
@ -74,4 +73,4 @@ impl From<(PeerId, Message)> for bob::OutEvent {
}
}
}
crate::impl_from_rr_event!(OutEvent, bob::OutEvent, PROTOCOL);
crate::impl_from_rr_event!(OutEvent, cli::OutEvent, PROTOCOL);

@ -2,31 +2,19 @@
//! Alice holds XMR and wishes receive BTC.
use crate::database::Database;
use crate::env::Config;
use crate::{bitcoin, monero};
use crate::{asb, bitcoin, monero};
use std::sync::Arc;
use uuid::Uuid;
pub use self::behaviour::{Behaviour, OutEvent};
pub use self::event_loop::{EventLoop, EventLoopHandle};
pub use self::recovery::cancel::cancel;
pub use self::recovery::punish::punish;
pub use self::recovery::redeem::redeem;
pub use self::recovery::refund::refund;
pub use self::recovery::safely_abort::safely_abort;
pub use self::recovery::{cancel, punish, redeem, refund, safely_abort};
pub use self::state::*;
pub use self::swap::{run, run_until};
mod behaviour;
pub mod event_loop;
mod recovery;
pub mod state;
pub mod swap;
pub mod swap_setup;
pub struct Swap {
pub state: AliceState,
pub event_loop_handle: EventLoopHandle,
pub event_loop_handle: asb::EventLoopHandle,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
pub env_config: Config,

@ -1,8 +1,8 @@
//! Run an XMR/BTC swap in the role of Alice.
//! Alice holds XMR and wishes receive BTC.
use crate::asb::{EventLoopHandle, LatestRate};
use crate::bitcoin::ExpiredTimelocks;
use crate::env::Config;
use crate::protocol::alice::event_loop::{EventLoopHandle, LatestRate};
use crate::protocol::alice::{AliceState, Swap};
use crate::{bitcoin, database, monero};
use anyhow::{bail, Context, Result};

@ -1,27 +1,20 @@
use crate::database::Database;
use crate::{bitcoin, env, monero};
use anyhow::Result;
use std::sync::Arc;
use anyhow::Result;
use uuid::Uuid;
pub use self::behaviour::{Behaviour, OutEvent};
pub use self::cancel::cancel;
pub use self::event_loop::{EventLoop, EventLoopHandle};
pub use self::refund::refund;
use crate::database::Database;
use crate::{bitcoin, cli, env, monero};
pub use self::state::*;
pub use self::swap::{run, run_until};
mod behaviour;
pub mod cancel;
pub mod event_loop;
pub mod refund;
pub mod state;
pub mod swap;
mod swap_setup;
pub struct Swap {
pub state: BobState,
pub event_loop_handle: EventLoopHandle,
pub event_loop_handle: cli::EventLoopHandle,
pub db: Database,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
@ -38,7 +31,7 @@ impl Swap {
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
env_config: env::Config,
event_loop_handle: EventLoopHandle,
event_loop_handle: cli::EventLoopHandle,
receive_monero_address: monero::Address,
btc_amount: bitcoin::Amount,
) -> Self {
@ -60,7 +53,7 @@ impl Swap {
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
env_config: env::Config,
event_loop_handle: EventLoopHandle,
event_loop_handle: cli::EventLoopHandle,
receive_monero_address: monero::Address,
) -> Result<Self> {
let state = db.get_state(id)?.try_into_bob()?.into();

@ -1,9 +1,9 @@
use crate::bitcoin::{ExpiredTimelocks, TxCancel, TxRefund};
use crate::cli::EventLoopHandle;
use crate::database::Swap;
use crate::network::swap_setup::bob::NewSwap;
use crate::protocol::bob;
use crate::protocol::bob::event_loop::EventLoopHandle;
use crate::protocol::bob::state::*;
use crate::protocol::bob::swap_setup::NewSwap;
use crate::{bitcoin, monero};
use anyhow::{bail, Context, Result};
use tokio::select;

@ -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,

@ -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;

@ -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;

@ -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,

@ -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,

@ -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};

@ -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};

@ -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};

@ -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};

@ -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;

@ -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};

@ -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};

@ -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};

@ -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)
}
}

@ -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};

Loading…
Cancel
Save