mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-25 07:29:32 -05:00
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:
parent
0c19af9090
commit
67e925fe1f
@ -330,11 +330,19 @@ async fn bob_swap(
|
||||
alice_addr: Multiaddr,
|
||||
seed: Seed,
|
||||
) -> Result<BobState> {
|
||||
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,
|
||||
|
@ -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<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)]
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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(),
|
||||
}))
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user