From 0c19af9090ded142ba8328b08e1b34cb641fc312 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 18 Jan 2021 20:33:10 +1100 Subject: [PATCH] Refactor Alice'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 | 13 +++-- swap/src/protocol/alice.rs | 69 ++++++--------------------- swap/src/protocol/alice/event_loop.rs | 9 +--- swap/tests/testutils/mod.rs | 10 ++-- 4 files changed, 31 insertions(+), 70 deletions(-) diff --git a/swap/src/main.rs b/swap/src/main.rs index 8e692839..f04efc48 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -292,13 +292,16 @@ async fn alice_swap( db: Database, seed: Seed, ) -> Result { - let alice_behaviour = alice::Behaviour::new(network::Seed::new(seed)); - let alice_peer_id = alice_behaviour.peer_id(); - info!("Own Peer-ID: {}", alice_peer_id); - let alice_transport = build(alice_behaviour.identity())?; + let identity = network::Seed::new(seed).derive_libp2p_identity(); + + let peer_id = identity.public().into_peer_id(); + + let alice_behaviour = alice::Behaviour::default(); + info!("Own Peer-ID: {}", peer_id); + let alice_transport = build(identity)?; let (mut event_loop, handle) = - alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen_addr)?; + alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen_addr, peer_id)?; let swap = alice::Swap { state, diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 65419783..a3b12fb5 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -1,11 +1,6 @@ //! Run an XMR/BTC swap in the role of Alice. //! Alice holds XMR and wishes receive BTC. -use anyhow::Result; -use libp2p::{ - core::{identity::Keypair, Multiaddr}, - request_response::ResponseChannel, - NetworkBehaviour, PeerId, -}; +use libp2p::{request_response::ResponseChannel, NetworkBehaviour, PeerId}; use tracing::{debug, info}; use crate::{ @@ -13,8 +8,6 @@ use crate::{ network::{ peer_tracker::{self, PeerTracker}, request_response::AliceToBob, - transport::SwapTransport, - Seed, TokioExecutor, }, protocol::bob, SwapAmounts, @@ -55,29 +48,6 @@ pub struct Swap { pub type Swarm = libp2p::Swarm; -pub fn new_swarm( - listen: Multiaddr, - transport: SwapTransport, - behaviour: Behaviour, -) -> Result { - use anyhow::Context as _; - - let local_peer_id = behaviour.peer_id(); - - let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id.clone()) - .executor(Box::new(TokioExecutor { - handle: tokio::runtime::Handle::current(), - })) - .build(); - - Swarm::listen_on(&mut swarm, listen.clone()) - .with_context(|| format!("Address is not supported: {:#}", listen))?; - - tracing::info!("Initialized swarm: {}", local_peer_id); - - Ok(swarm) -} - #[derive(Debug)] pub enum OutEvent { ConnectionEstablished(PeerId), @@ -162,33 +132,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()) - } - /// Alice always sends her messages as a response to a request from Bob. pub fn send_amounts(&mut self, channel: ResponseChannel, amounts: SwapAmounts) { let msg = AliceToBob::Amounts(amounts); @@ -214,3 +160,16 @@ impl Behaviour { debug!("Sent Message2"); } } + +impl Default for Behaviour { + fn default() -> Self { + Self { + pt: PeerTracker::default(), + amounts: Amounts::default(), + message0: message0::Behaviour::default(), + message1: message1::Behaviour::default(), + message2: message2::Behaviour::default(), + message3: message3::Behaviour::default(), + } + } +} diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index 3b2f655b..db5eb67f 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -148,10 +148,9 @@ impl EventLoop { transport: SwapTransport, behaviour: Behaviour, listen: Multiaddr, + peer_id: PeerId, ) -> 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(), })) @@ -249,8 +248,4 @@ impl EventLoop { } } } - - pub fn peer_id(&self) -> PeerId { - self.swarm.peer_id() - } } diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index ef935769..426ea706 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -666,9 +666,13 @@ fn init_alice_event_loop( alice::event_loop::EventLoop, alice::event_loop::EventLoopHandle, ) { - let alice_behaviour = alice::Behaviour::new(network::Seed::new(seed)); - let alice_transport = build(alice_behaviour.identity()).unwrap(); - alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap() + let identity = network::Seed::new(seed).derive_libp2p_identity(); + + let peer_id = identity.public().into_peer_id(); + + let alice_behaviour = alice::Behaviour::default(); + let alice_transport = build(identity).unwrap(); + alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen, peer_id).unwrap() } async fn init_bob_state(