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.
rendezvous-asb-refactor
Daniel Karzel 3 years ago
parent 2e3601693e
commit 4ffb2f14be
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E

@ -86,14 +86,9 @@ async fn main() -> Result<()> {
init_monero_wallet(data_dir, monero_daemon_address, env_config).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet);
let mut swarm = swarm::cli(
&seed,
seller_peer_id,
tor_socks5_port,
env_config,
bitcoin_wallet.clone(),
)
.await?;
let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
let mut swarm =
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
swarm
.behaviour_mut()
.add_address(seller_peer_id, seller_addr);
@ -188,14 +183,9 @@ async fn main() -> Result<()> {
let seller_peer_id = db.get_peer_id(swap_id)?;
let mut swarm = swarm::cli(
&seed,
seller_peer_id,
tor_socks5_port,
env_config,
bitcoin_wallet.clone(),
)
.await?;
let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
let mut swarm =
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
let our_peer_id = swarm.local_peer_id();
tracing::debug!(peer_id = %our_peer_id, "Initializing network module");
swarm

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

@ -445,12 +445,16 @@ impl BobParams {
) -> 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(
&self.seed,
let behaviour = cli::Behaviour::new(
self.alice_peer_id,
tor_socks5_port,
self.env_config,
self.bitcoin_wallet.clone(),
);
let mut swarm = swarm::cli(
self.seed.derive_libp2p_identity(),
tor_socks5_port,
behaviour,
)
.await?;
swarm

Loading…
Cancel
Save