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.
This commit is contained in:
Daniel Karzel 2021-01-18 20:41:58 +11:00
parent 0c19af9090
commit 67e925fe1f
4 changed files with 39 additions and 63 deletions

View File

@ -330,11 +330,19 @@ async fn bob_swap(
alice_addr: Multiaddr, alice_addr: Multiaddr,
seed: Seed, seed: Seed,
) -> Result<BobState> { ) -> Result<BobState> {
let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed)); let identity = network::Seed::new(seed).derive_libp2p_identity();
let bob_transport = build(bob_behaviour.identity())?; let peer_id = identity.public().into_peer_id();
let (event_loop, handle) = let bob_behaviour = bob::Behaviour::default();
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr)?; 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 { let swap = bob::Swap {
state, state,

View File

@ -1,21 +1,13 @@
//! Run an XMR/BTC swap in the role of Bob. //! Run an XMR/BTC swap in the role of Bob.
//! Bob holds BTC and wishes receive XMR. //! Bob holds BTC and wishes receive XMR.
use anyhow::Result; use libp2p::{core::Multiaddr, NetworkBehaviour, PeerId};
use libp2p::{
core::{identity::Keypair, Multiaddr},
NetworkBehaviour, PeerId,
};
use tracing::{debug, info}; use tracing::{debug, info};
use crate::{ use crate::{
bitcoin, bitcoin,
bitcoin::EncryptedSignature, bitcoin::EncryptedSignature,
monero, monero,
network::{ network::peer_tracker::{self, PeerTracker},
peer_tracker::{self, PeerTracker},
transport::SwapTransport,
Seed, TokioExecutor,
},
protocol::{alice, bob}, protocol::{alice, bob},
SwapAmounts, SwapAmounts,
}; };
@ -54,20 +46,6 @@ pub struct Swap {
pub type Swarm = libp2p::Swarm<Behaviour>; pub type Swarm = libp2p::Swarm<Behaviour>;
pub fn new_swarm(transport: SwapTransport, behaviour: Behaviour) -> Result<Swarm> {
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)] #[derive(Debug, Clone)]
pub enum OutEvent { pub enum OutEvent {
ConnectionEstablished(PeerId), ConnectionEstablished(PeerId),
@ -139,33 +117,9 @@ pub struct Behaviour {
message1: message1::Behaviour, message1: message1::Behaviour,
message2: message2::Behaviour, message2: message2::Behaviour,
message3: message3::Behaviour, message3: message3::Behaviour,
#[behaviour(ignore)]
identity: Keypair,
} }
impl Behaviour { 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`. /// Sends a message to Alice to get current amounts based on `btc`.
pub fn request_amounts(&mut self, alice: PeerId, btc: u64) { pub fn request_amounts(&mut self, alice: PeerId, btc: u64) {
let btc = ::bitcoin::Amount::from_sat(btc); let btc = ::bitcoin::Amount::from_sat(btc);
@ -206,8 +160,6 @@ impl Behaviour {
impl Default for Behaviour { impl Default for Behaviour {
fn default() -> Behaviour { fn default() -> Behaviour {
let identity = Keypair::generate_ed25519();
Self { Self {
pt: PeerTracker::default(), pt: PeerTracker::default(),
amounts: Amounts::default(), amounts: Amounts::default(),
@ -215,7 +167,6 @@ impl Default for Behaviour {
message1: message1::Behaviour::default(), message1: message1::Behaviour::default(),
message2: message2::Behaviour::default(), message2: message2::Behaviour::default(),
message3: message3::Behaviour::default(), message3: message3::Behaviour::default(),
identity,
} }
} }
} }

View File

@ -131,12 +131,11 @@ impl EventLoop {
pub fn new( pub fn new(
transport: SwapTransport, transport: SwapTransport,
behaviour: Behaviour, behaviour: Behaviour,
peer_id: PeerId,
alice_peer_id: PeerId, alice_peer_id: PeerId,
alice_addr: Multiaddr, alice_addr: Multiaddr,
) -> Result<(Self, EventLoopHandle)> { ) -> Result<(Self, EventLoopHandle)> {
let local_peer_id = behaviour.peer_id(); let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id)
.executor(Box::new(TokioExecutor { .executor(Box::new(TokioExecutor {
handle: tokio::runtime::Handle::current(), handle: tokio::runtime::Handle::current(),
})) }))

View File

@ -334,7 +334,10 @@ where
btc: swap_amounts.btc * 10, btc: swap_amounts.btc * 10,
}; };
let bob_seed = Seed::random().unwrap();
let bob_swap_factory = BobSwapFactory::new( let bob_swap_factory = BobSwapFactory::new(
bob_seed,
config, config,
Uuid::new_v4(), Uuid::new_v4(),
&monero, &monero,
@ -475,6 +478,8 @@ impl AliceSwapFactory {
} }
pub struct BobSwapFactory { pub struct BobSwapFactory {
seed: Seed,
db_path: PathBuf, db_path: PathBuf,
swap_id: Uuid, swap_id: Uuid,
@ -490,6 +495,7 @@ pub struct BobSwapFactory {
impl BobSwapFactory { impl BobSwapFactory {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
async fn new( async fn new(
seed: Seed,
config: Config, config: Config,
swap_id: Uuid, swap_id: Uuid,
monero: &Monero, monero: &Monero,
@ -504,6 +510,7 @@ impl BobSwapFactory {
init_wallets("bob", bitcoind, monero, starting_balances.clone(), config).await; init_wallets("bob", bitcoind, monero, starting_balances.clone(), config).await;
Self { Self {
seed,
db_path, db_path,
swap_id, swap_id,
bitcoin_wallet, bitcoin_wallet,
@ -525,6 +532,7 @@ impl BobSwapFactory {
.await; .await;
let (event_loop, event_loop_handle) = init_bob_event_loop( let (event_loop, event_loop_handle) = init_bob_event_loop(
self.seed,
self.alice_connect_peer_id.clone(), self.alice_connect_peer_id.clone(),
self.alice_connect_address.clone(), self.alice_connect_address.clone(),
); );
@ -556,6 +564,7 @@ impl BobSwapFactory {
}; };
let (event_loop, event_loop_handle) = init_bob_event_loop( let (event_loop, event_loop_handle) = init_bob_event_loop(
self.seed,
self.alice_connect_peer_id.clone(), self.alice_connect_peer_id.clone(),
self.alice_connect_address.clone(), self.alice_connect_address.clone(),
); );
@ -701,14 +710,23 @@ async fn init_bob_state(
} }
fn init_bob_event_loop( fn init_bob_event_loop(
seed: Seed,
alice_peer_id: PeerId, alice_peer_id: PeerId,
alice_addr: Multiaddr, alice_addr: Multiaddr,
) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { ) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) {
let seed = Seed::random().unwrap(); let identity = network::Seed::new(seed).derive_libp2p_identity();
let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed)); let peer_id = identity.public().into_peer_id();
let bob_transport = build(bob_behaviour.identity()).unwrap();
bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr) let bob_behaviour = bob::Behaviour::default();
.unwrap() 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 // This is just to keep the containers alive