Introduce swarm::{alice,bob} function to construct a Swarm instance

This commit is contained in:
Thomas Eizinger 2021-03-26 16:13:47 +11:00
parent 19766b9759
commit d4c10a1292
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
4 changed files with 17 additions and 8 deletions

View File

@ -27,7 +27,7 @@ use swap::fs::default_config_path;
use swap::monero::Amount; use swap::monero::Amount;
use swap::network::swarm; use swap::network::swarm;
use swap::protocol::alice::event_loop::KrakenRate; 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::seed::Seed;
use swap::{asb, bitcoin, env, kraken, monero}; use swap::{asb, bitcoin, env, kraken, monero};
use tracing::{info, warn}; use tracing::{info, warn};
@ -97,7 +97,7 @@ async fn main() -> Result<()> {
let kraken_price_updates = kraken::connect()?; let kraken_price_updates = kraken::connect()?;
let mut swarm = swarm::new::<Behaviour>(&seed)?; let mut swarm = swarm::alice(&seed)?;
Swarm::listen_on(&mut swarm, config.network.listen) Swarm::listen_on(&mut swarm, config.network.listen)
.context("Failed to listen network interface")?; .context("Failed to listen network interface")?;

View File

@ -27,7 +27,7 @@ use swap::env::{Config, GetConfig};
use swap::network::quote::BidQuote; use swap::network::quote::BidQuote;
use swap::network::swarm; use swap::network::swarm;
use swap::protocol::bob; use swap::protocol::bob;
use swap::protocol::bob::{Behaviour, Builder, EventLoop}; use swap::protocol::bob::{Builder, EventLoop};
use swap::seed::Seed; use swap::seed::Seed;
use swap::{bitcoin, cli, env, monero}; use swap::{bitcoin, cli, env, monero};
use tracing::{debug, error, info, warn}; 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?; init_monero_wallet(data_dir, monero_daemon_host, env_config).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet); let bitcoin_wallet = Arc::new(bitcoin_wallet);
let mut swarm = swarm::new::<Behaviour>(&seed)?; let mut swarm = swarm::bob(&seed)?;
swarm.add_address(alice_peer_id, alice_multiaddr); swarm.add_address(alice_peer_id, alice_multiaddr);
let swap_id = Uuid::new_v4(); let swap_id = Uuid::new_v4();
@ -171,7 +171,7 @@ async fn main() -> Result<()> {
let bitcoin_wallet = Arc::new(bitcoin_wallet); let bitcoin_wallet = Arc::new(bitcoin_wallet);
let alice_peer_id = db.get_peer_id(swap_id)?; let alice_peer_id = db.get_peer_id(swap_id)?;
let mut swarm = swarm::new::<Behaviour>(&seed)?; let mut swarm = swarm::bob(&seed)?;
swarm.add_address(alice_peer_id, alice_multiaddr); swarm.add_address(alice_peer_id, alice_multiaddr);
let (event_loop, event_loop_handle) = let (event_loop, event_loop_handle) =

View File

@ -1,10 +1,19 @@
use crate::network::transport; use crate::network::transport;
use crate::protocol::{alice, bob};
use crate::seed::Seed; use crate::seed::Seed;
use anyhow::Result; use anyhow::Result;
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder}; use libp2p::swarm::{NetworkBehaviour, SwarmBuilder};
use libp2p::Swarm; use libp2p::Swarm;
pub fn new<B>(seed: &Seed) -> Result<Swarm<B>> pub fn alice(seed: &Seed) -> Result<Swarm<alice::Behaviour>> {
new(seed)
}
pub fn bob(seed: &Seed) -> Result<Swarm<bob::Behaviour>> {
new(seed)
}
fn new<B>(seed: &Seed) -> Result<Swarm<B>>
where where
B: NetworkBehaviour + Default, B: NetworkBehaviour + Default,
{ {

View File

@ -116,7 +116,7 @@ impl BobParams {
} }
pub fn new_eventloop(&self, swap_id: Uuid) -> Result<(bob::EventLoop, bob::EventLoopHandle)> { pub fn new_eventloop(&self, swap_id: Uuid) -> Result<(bob::EventLoop, bob::EventLoopHandle)> {
let mut swarm = swarm::new::<bob::Behaviour>(&self.seed)?; let mut swarm = swarm::bob(&self.seed)?;
swarm.add_address(self.alice_peer_id, self.alice_address.clone()); swarm.add_address(self.alice_peer_id, self.alice_address.clone());
bob::EventLoop::new( bob::EventLoop::new(
@ -641,7 +641,7 @@ fn start_alice(
) -> (AliceApplicationHandle, Receiver<alice::Swap>) { ) -> (AliceApplicationHandle, Receiver<alice::Swap>) {
let db = Arc::new(Database::open(db_path.as_path()).unwrap()); let db = Arc::new(Database::open(db_path.as_path()).unwrap());
let mut swarm = swarm::new::<alice::Behaviour>(&seed).unwrap(); let mut swarm = swarm::alice(&seed).unwrap();
Swarm::listen_on(&mut swarm, listen_address).unwrap(); Swarm::listen_on(&mut swarm, listen_address).unwrap();
let (event_loop, swap_handle) = alice::EventLoop::new( let (event_loop, swap_handle) = alice::EventLoop::new(