From d4c10a1292446b001b40d21a6e566dbd47f5a9af Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 26 Mar 2021 16:13:47 +1100 Subject: [PATCH] Introduce swarm::{alice,bob} function to construct a Swarm instance --- swap/src/bin/asb.rs | 4 ++-- swap/src/bin/swap.rs | 6 +++--- swap/src/network/swarm.rs | 11 ++++++++++- swap/tests/harness/mod.rs | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 61908b92..eee8951f 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -27,7 +27,7 @@ use swap::fs::default_config_path; use swap::monero::Amount; use swap::network::swarm; use swap::protocol::alice::event_loop::KrakenRate; -use swap::protocol::alice::{run, Behaviour, EventLoop}; +use swap::protocol::alice::{run, EventLoop}; use swap::seed::Seed; use swap::{asb, bitcoin, env, kraken, monero}; use tracing::{info, warn}; @@ -97,7 +97,7 @@ async fn main() -> Result<()> { let kraken_price_updates = kraken::connect()?; - let mut swarm = swarm::new::(&seed)?; + let mut swarm = swarm::alice(&seed)?; Swarm::listen_on(&mut swarm, config.network.listen) .context("Failed to listen network interface")?; diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index a898087f..f815fb02 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -27,7 +27,7 @@ use swap::env::{Config, GetConfig}; use swap::network::quote::BidQuote; use swap::network::swarm; use swap::protocol::bob; -use swap::protocol::bob::{Behaviour, Builder, EventLoop}; +use swap::protocol::bob::{Builder, EventLoop}; use swap::seed::Seed; use swap::{bitcoin, cli, env, monero}; use tracing::{debug, error, info, warn}; @@ -76,7 +76,7 @@ async fn main() -> Result<()> { init_monero_wallet(data_dir, monero_daemon_host, env_config).await?; let bitcoin_wallet = Arc::new(bitcoin_wallet); - let mut swarm = swarm::new::(&seed)?; + let mut swarm = swarm::bob(&seed)?; swarm.add_address(alice_peer_id, alice_multiaddr); let swap_id = Uuid::new_v4(); @@ -171,7 +171,7 @@ async fn main() -> Result<()> { let bitcoin_wallet = Arc::new(bitcoin_wallet); let alice_peer_id = db.get_peer_id(swap_id)?; - let mut swarm = swarm::new::(&seed)?; + let mut swarm = swarm::bob(&seed)?; swarm.add_address(alice_peer_id, alice_multiaddr); let (event_loop, event_loop_handle) = diff --git a/swap/src/network/swarm.rs b/swap/src/network/swarm.rs index 40e9f6d4..220d8156 100644 --- a/swap/src/network/swarm.rs +++ b/swap/src/network/swarm.rs @@ -1,10 +1,19 @@ use crate::network::transport; +use crate::protocol::{alice, bob}; use crate::seed::Seed; use anyhow::Result; use libp2p::swarm::{NetworkBehaviour, SwarmBuilder}; use libp2p::Swarm; -pub fn new(seed: &Seed) -> Result> +pub fn alice(seed: &Seed) -> Result> { + new(seed) +} + +pub fn bob(seed: &Seed) -> Result> { + new(seed) +} + +fn new(seed: &Seed) -> Result> where B: NetworkBehaviour + Default, { diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index bdbf3bd9..ada61805 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -116,7 +116,7 @@ impl BobParams { } pub fn new_eventloop(&self, swap_id: Uuid) -> Result<(bob::EventLoop, bob::EventLoopHandle)> { - let mut swarm = swarm::new::(&self.seed)?; + let mut swarm = swarm::bob(&self.seed)?; swarm.add_address(self.alice_peer_id, self.alice_address.clone()); bob::EventLoop::new( @@ -641,7 +641,7 @@ fn start_alice( ) -> (AliceApplicationHandle, Receiver) { let db = Arc::new(Database::open(db_path.as_path()).unwrap()); - let mut swarm = swarm::new::(&seed).unwrap(); + let mut swarm = swarm::alice(&seed).unwrap(); Swarm::listen_on(&mut swarm, listen_address).unwrap(); let (event_loop, swap_handle) = alice::EventLoop::new(