mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
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.
This commit is contained in:
parent
8bf467b550
commit
0c19af9090
@ -292,13 +292,16 @@ async fn alice_swap(
|
||||
db: Database,
|
||||
seed: Seed,
|
||||
) -> Result<AliceState> {
|
||||
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,
|
||||
|
@ -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<Behaviour>;
|
||||
|
||||
pub fn new_swarm(
|
||||
listen: Multiaddr,
|
||||
transport: SwapTransport,
|
||||
behaviour: Behaviour,
|
||||
) -> Result<Swarm> {
|
||||
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<AliceToBob>, 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user