From 089ac0806efa9b3a40a2f5ffc86c58cc99e1853c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 3 Mar 2021 13:05:18 +1100 Subject: [PATCH] Simplify constructor of Bob's EventLoop We never customize the behaviour or transport. Might as well hide those details in the implementation. --- swap/src/protocol/bob.rs | 15 ++------------- swap/src/protocol/bob/event_loop.rs | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index f63d358c..ff815260 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -5,10 +5,7 @@ use crate::{ database::Database, execution_params::ExecutionParams, monero, network, - network::{ - peer_tracker::{self, PeerTracker}, - transport::build, - }, + network::peer_tracker::{self, PeerTracker}, protocol::{alice, alice::TransferProof, bob}, seed::Seed, }; @@ -53,7 +50,6 @@ pub struct Swap { pub struct Builder { swap_id: Uuid, identity: Keypair, - peer_id: PeerId, db: Database, alice_address: Multiaddr, @@ -84,12 +80,10 @@ impl Builder { execution_params: ExecutionParams, ) -> Self { let identity = network::Seed::new(seed).derive_libp2p_identity(); - let peer_id = identity.public().into_peer_id(); Self { swap_id, identity, - peer_id, db, alice_address, alice_peer_id, @@ -152,13 +146,8 @@ impl Builder { fn init_event_loop( &self, ) -> Result<(bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle)> { - let bob_behaviour = bob::Behaviour::default(); - let bob_transport = build(&self.identity)?; - bob::event_loop::EventLoop::new( - bob_transport, - bob_behaviour, - self.peer_id, + &self.identity, self.alice_peer_id, self.alice_address.clone(), self.bitcoin_wallet.clone(), diff --git a/swap/src/protocol/bob/event_loop.rs b/swap/src/protocol/bob/event_loop.rs index b7e222a7..44011600 100644 --- a/swap/src/protocol/bob/event_loop.rs +++ b/swap/src/protocol/bob/event_loop.rs @@ -1,7 +1,7 @@ use crate::{ bitcoin, bitcoin::EncryptedSignature, - network::{transport::SwapTransport, TokioExecutor}, + network::{transport, TokioExecutor}, protocol::{ alice::{QuoteResponse, TransferProof}, bob::{Behaviour, OutEvent, QuoteRequest, State0, State2}, @@ -114,18 +114,23 @@ pub struct EventLoop { impl EventLoop { pub fn new( - transport: SwapTransport, - behaviour: Behaviour, - peer_id: PeerId, + identity: &libp2p::core::identity::Keypair, alice_peer_id: PeerId, alice_addr: Multiaddr, bitcoin_wallet: Arc, ) -> Result<(Self, EventLoopHandle)> { - let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id) - .executor(Box::new(TokioExecutor { - handle: tokio::runtime::Handle::current(), - })) - .build(); + let behaviour = Behaviour::default(); + let transport = transport::build(identity)?; + + let mut swarm = libp2p::swarm::SwarmBuilder::new( + transport, + behaviour, + identity.public().into_peer_id(), + ) + .executor(Box::new(TokioExecutor { + handle: tokio::runtime::Handle::current(), + })) + .build(); swarm.add_address(alice_peer_id, alice_addr);