Pass in CLI behaviour when creating swarm

This allows us to reuse the swarm setup with a different behaviour for discovering ASBs with a rendezvous node.
This commit is contained in:
Daniel Karzel 2021-06-28 20:49:02 +10:00 committed by Thomas Eizinger
parent 4cd27e372c
commit ff8cca2e27
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
3 changed files with 22 additions and 31 deletions

View File

@ -91,14 +91,9 @@ async fn main() -> Result<()> {
.context("Seller address must contain peer ID")?; .context("Seller address must contain peer ID")?;
db.insert_address(seller_peer_id, seller.clone()).await?; db.insert_address(seller_peer_id, seller.clone()).await?;
let mut swarm = swarm::cli( let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
&seed, let mut swarm =
seller_peer_id, swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
tor_socks5_port,
env_config,
bitcoin_wallet.clone(),
)
.await?;
swarm.behaviour_mut().add_address(seller_peer_id, seller); swarm.behaviour_mut().add_address(seller_peer_id, seller);
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized"); tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
@ -191,14 +186,9 @@ async fn main() -> Result<()> {
let seller_peer_id = db.get_peer_id(swap_id)?; let seller_peer_id = db.get_peer_id(swap_id)?;
let seller_addresses = db.get_addresses(seller_peer_id)?; let seller_addresses = db.get_addresses(seller_peer_id)?;
let mut swarm = swarm::cli( let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
&seed, let mut swarm =
seller_peer_id, swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
tor_socks5_port,
env_config,
bitcoin_wallet.clone(),
)
.await?;
let our_peer_id = swarm.local_peer_id(); let our_peer_id = swarm.local_peer_id();
tracing::debug!(peer_id = %our_peer_id, "Initializing network module"); tracing::debug!(peer_id = %our_peer_id, "Initializing network module");

View File

@ -2,10 +2,9 @@ use crate::asb::LatestRate;
use crate::seed::Seed; use crate::seed::Seed;
use crate::{asb, bitcoin, cli, env, tor}; use crate::{asb, bitcoin, cli, env, tor};
use anyhow::Result; use anyhow::Result;
use libp2p::swarm::SwarmBuilder; use libp2p::swarm::{NetworkBehaviour, SwarmBuilder};
use libp2p::{PeerId, Swarm}; use libp2p::{identity, Swarm};
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc;
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn asb<LR>( pub fn asb<LR>(
@ -34,21 +33,19 @@ where
Ok(swarm) Ok(swarm)
} }
pub async fn cli( pub async fn cli<T>(
seed: &Seed, identity: identity::Keypair,
alice: PeerId,
tor_socks5_port: u16, tor_socks5_port: u16,
env_config: env::Config, behaviour: T,
bitcoin_wallet: Arc<bitcoin::Wallet>, ) -> Result<Swarm<T>>
) -> Result<Swarm<cli::Behaviour>> { where
T: NetworkBehaviour,
{
let maybe_tor_socks5_port = match tor::Client::new(tor_socks5_port).assert_tor_running().await { let maybe_tor_socks5_port = match tor::Client::new(tor_socks5_port).assert_tor_running().await {
Ok(()) => Some(tor_socks5_port), Ok(()) => Some(tor_socks5_port),
Err(_) => None, Err(_) => None,
}; };
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)?; let transport = cli::transport::new(&identity, maybe_tor_socks5_port)?;
let peer_id = identity.public().into_peer_id(); let peer_id = identity.public().into_peer_id();

View File

@ -445,12 +445,16 @@ impl BobParams {
) -> Result<(cli::EventLoop, cli::EventLoopHandle)> { ) -> Result<(cli::EventLoop, cli::EventLoopHandle)> {
let tor_socks5_port = get_port() 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."); .expect("We don't care about Tor in the tests so we get a free port to disable it.");
let mut swarm = swarm::cli(
&self.seed, let behaviour = cli::Behaviour::new(
self.alice_peer_id, self.alice_peer_id,
tor_socks5_port,
self.env_config, self.env_config,
self.bitcoin_wallet.clone(), self.bitcoin_wallet.clone(),
);
let mut swarm = swarm::cli(
self.seed.derive_libp2p_identity(),
tor_socks5_port,
behaviour,
) )
.await?; .await?;
swarm swarm