From 67e925fe1ff58b8174ef7a4560fb89d5a3f435c8 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 18 Jan 2021 20:41:58 +1100 Subject: [PATCH] Refactor Bob's peer-id and identity to be handled on the outside Doing this in the behaviour is a weird indirection that is not needed. --- swap/src/main.rs | 16 ++++++--- swap/src/protocol/bob.rs | 53 ++--------------------------- swap/src/protocol/bob/event_loop.rs | 5 ++- swap/tests/testutils/mod.rs | 28 ++++++++++++--- 4 files changed, 39 insertions(+), 63 deletions(-) diff --git a/swap/src/main.rs b/swap/src/main.rs index f04efc48..d9a339bb 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -330,11 +330,19 @@ async fn bob_swap( alice_addr: Multiaddr, seed: Seed, ) -> Result { - let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed)); - let bob_transport = build(bob_behaviour.identity())?; + let identity = network::Seed::new(seed).derive_libp2p_identity(); + let peer_id = identity.public().into_peer_id(); - let (event_loop, handle) = - bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)?; + let bob_behaviour = bob::Behaviour::default(); + let bob_transport = build(identity)?; + + let (event_loop, handle) = bob::event_loop::EventLoop::new( + bob_transport, + bob_behaviour, + peer_id, + alice_peer_id, + alice_addr, + )?; let swap = bob::Swap { state, diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index 5558d616..abee4425 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -1,21 +1,13 @@ //! Run an XMR/BTC swap in the role of Bob. //! Bob holds BTC and wishes receive XMR. -use anyhow::Result; -use libp2p::{ - core::{identity::Keypair, Multiaddr}, - NetworkBehaviour, PeerId, -}; +use libp2p::{core::Multiaddr, NetworkBehaviour, PeerId}; use tracing::{debug, info}; use crate::{ bitcoin, bitcoin::EncryptedSignature, monero, - network::{ - peer_tracker::{self, PeerTracker}, - transport::SwapTransport, - Seed, TokioExecutor, - }, + network::peer_tracker::{self, PeerTracker}, protocol::{alice, bob}, SwapAmounts, }; @@ -54,20 +46,6 @@ pub struct Swap { pub type Swarm = libp2p::Swarm; -pub fn new_swarm(transport: SwapTransport, behaviour: Behaviour) -> Result { - let local_peer_id = behaviour.peer_id(); - - let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id.clone()) - .executor(Box::new(TokioExecutor { - handle: tokio::runtime::Handle::current(), - })) - .build(); - - info!("Initialized swarm with identity {}", local_peer_id); - - Ok(swarm) -} - #[derive(Debug, Clone)] pub enum OutEvent { ConnectionEstablished(PeerId), @@ -139,33 +117,9 @@ pub struct Behaviour { message1: message1::Behaviour, message2: message2::Behaviour, message3: message3::Behaviour, - #[behaviour(ignore)] - identity: Keypair, } impl Behaviour { - pub fn new(seed: Seed) -> Self { - let identity = seed.derive_libp2p_identity(); - - Self { - pt: PeerTracker::default(), - amounts: Amounts::default(), - message0: message0::Behaviour::default(), - message1: message1::Behaviour::default(), - message2: message2::Behaviour::default(), - message3: message3::Behaviour::default(), - identity, - } - } - - pub fn identity(&self) -> Keypair { - self.identity.clone() - } - - pub fn peer_id(&self) -> PeerId { - PeerId::from(self.identity.public()) - } - /// Sends a message to Alice to get current amounts based on `btc`. pub fn request_amounts(&mut self, alice: PeerId, btc: u64) { let btc = ::bitcoin::Amount::from_sat(btc); @@ -206,8 +160,6 @@ impl Behaviour { impl Default for Behaviour { fn default() -> Behaviour { - let identity = Keypair::generate_ed25519(); - Self { pt: PeerTracker::default(), amounts: Amounts::default(), @@ -215,7 +167,6 @@ impl Default for Behaviour { message1: message1::Behaviour::default(), message2: message2::Behaviour::default(), message3: message3::Behaviour::default(), - identity, } } } diff --git a/swap/src/protocol/bob/event_loop.rs b/swap/src/protocol/bob/event_loop.rs index b193a5f2..5e95356a 100644 --- a/swap/src/protocol/bob/event_loop.rs +++ b/swap/src/protocol/bob/event_loop.rs @@ -131,12 +131,11 @@ impl EventLoop { pub fn new( transport: SwapTransport, behaviour: Behaviour, + peer_id: PeerId, alice_peer_id: PeerId, alice_addr: Multiaddr, ) -> Result<(Self, EventLoopHandle)> { - let local_peer_id = behaviour.peer_id(); - - let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id) + let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id) .executor(Box::new(TokioExecutor { handle: tokio::runtime::Handle::current(), })) diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 426ea706..9fb22e1c 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -334,7 +334,10 @@ where btc: swap_amounts.btc * 10, }; + let bob_seed = Seed::random().unwrap(); + let bob_swap_factory = BobSwapFactory::new( + bob_seed, config, Uuid::new_v4(), &monero, @@ -475,6 +478,8 @@ impl AliceSwapFactory { } pub struct BobSwapFactory { + seed: Seed, + db_path: PathBuf, swap_id: Uuid, @@ -490,6 +495,7 @@ pub struct BobSwapFactory { impl BobSwapFactory { #[allow(clippy::too_many_arguments)] async fn new( + seed: Seed, config: Config, swap_id: Uuid, monero: &Monero, @@ -504,6 +510,7 @@ impl BobSwapFactory { init_wallets("bob", bitcoind, monero, starting_balances.clone(), config).await; Self { + seed, db_path, swap_id, bitcoin_wallet, @@ -525,6 +532,7 @@ impl BobSwapFactory { .await; let (event_loop, event_loop_handle) = init_bob_event_loop( + self.seed, self.alice_connect_peer_id.clone(), self.alice_connect_address.clone(), ); @@ -556,6 +564,7 @@ impl BobSwapFactory { }; let (event_loop, event_loop_handle) = init_bob_event_loop( + self.seed, self.alice_connect_peer_id.clone(), self.alice_connect_address.clone(), ); @@ -701,14 +710,23 @@ async fn init_bob_state( } fn init_bob_event_loop( + seed: Seed, alice_peer_id: PeerId, alice_addr: Multiaddr, ) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { - let seed = Seed::random().unwrap(); - let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed)); - let bob_transport = build(bob_behaviour.identity()).unwrap(); - bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr) - .unwrap() + let identity = network::Seed::new(seed).derive_libp2p_identity(); + let peer_id = identity.public().into_peer_id(); + + let bob_behaviour = bob::Behaviour::default(); + let bob_transport = build(identity).unwrap(); + bob::event_loop::EventLoop::new( + bob_transport, + bob_behaviour, + peer_id, + alice_peer_id, + alice_addr, + ) + .unwrap() } // This is just to keep the containers alive