Prepare separation of event loop initialisation to swap initialisation

As for Alice, the event loop will be started with the program and will
be the one starting swaps (`run_until`) based on libp2p events (swap
request).
This commit is contained in:
Franck Royer 2021-02-08 14:49:36 +11:00
parent 530b9b2ea8
commit 87be9aeb2a
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 9 additions and 17 deletions

View File

@ -7,7 +7,6 @@ use crate::{
monero, monero,
network::{ network::{
peer_tracker::{self, PeerTracker}, peer_tracker::{self, PeerTracker},
transport::build,
Seed as NetworkSeed, Seed as NetworkSeed,
}, },
protocol::{bob::EncryptedSignature, SwapAmounts}, protocol::{bob::EncryptedSignature, SwapAmounts},
@ -113,7 +112,8 @@ impl Builder {
.make_initial_state(swap_amounts.btc, swap_amounts.xmr) .make_initial_state(swap_amounts.btc, swap_amounts.xmr)
.await?; .await?;
let (event_loop, event_loop_handle) = self.init_event_loop()?; let (event_loop, event_loop_handle) =
EventLoop::new(self.identity.clone(), self.listen_address(), self.peer_id)?;
Ok(( Ok((
Swap { Swap {
@ -139,7 +139,8 @@ impl Builder {
) )
}; };
let (event_loop, event_loop_handle) = self.init_event_loop()?; let (event_loop, event_loop_handle) =
EventLoop::new(self.identity.clone(), self.listen_address(), self.peer_id)?;
Ok(( Ok((
Swap { Swap {
@ -186,17 +187,6 @@ impl Builder {
Ok(AliceState::Started { amounts, state0 }) Ok(AliceState::Started { amounts, state0 })
} }
fn init_event_loop(&self) -> Result<(EventLoop, EventLoopHandle)> {
let alice_behaviour = Behaviour::default();
let alice_transport = build(self.identity.clone())?;
EventLoop::new(
alice_transport,
alice_behaviour,
self.listen_address(),
self.peer_id,
)
}
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
execution_params::ExecutionParams, execution_params::ExecutionParams,
network::{transport::SwapTransport, TokioExecutor}, network::{transport, TokioExecutor},
protocol::{ protocol::{
alice::{Behaviour, OutEvent, State0, State3, SwapResponse, TransferProof}, alice::{Behaviour, OutEvent, State0, State3, SwapResponse, TransferProof},
bob::{EncryptedSignature, SwapRequest}, bob::{EncryptedSignature, SwapRequest},
@ -135,11 +135,13 @@ pub struct EventLoop {
impl EventLoop { impl EventLoop {
pub fn new( pub fn new(
transport: SwapTransport, identity: libp2p::identity::Keypair,
behaviour: Behaviour,
listen: Multiaddr, listen: Multiaddr,
peer_id: PeerId, peer_id: PeerId,
) -> Result<(Self, EventLoopHandle)> { ) -> Result<(Self, EventLoopHandle)> {
let behaviour = Behaviour::default();
let transport = transport::build(identity)?;
let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id) let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id)
.executor(Box::new(TokioExecutor { .executor(Box::new(TokioExecutor {
handle: tokio::runtime::Handle::current(), handle: tokio::runtime::Handle::current(),