Delete bob::negotiate

This module was intended to contain helper functions for each step.

However, those are not needed except for the negotiate step.
A dedicated module is not needed for one function.
This commit is contained in:
Franck Royer 2020-12-15 16:53:50 +11:00
parent 19066200d1
commit 71cf501c29
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 41 additions and 48 deletions

View File

@ -24,7 +24,6 @@ mod message0;
mod message1;
mod message2;
mod message3;
mod negotiate;
pub mod swap;
pub type Swarm = libp2p::Swarm<Behaviour>;

View File

@ -1,45 +0,0 @@
use crate::{bob::event_loop::EventLoopHandle, SwapAmounts};
use anyhow::Result;
use libp2p::{core::Multiaddr, PeerId};
use rand::{CryptoRng, RngCore};
use std::sync::Arc;
use xmr_btc::bob::State2;
pub async fn negotiate<R>(
state0: xmr_btc::bob::State0,
amounts: SwapAmounts,
swarm: &mut EventLoopHandle,
addr: Multiaddr,
mut rng: R,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
) -> Result<(State2, PeerId)>
where
R: RngCore + CryptoRng + Send,
{
tracing::trace!("Starting negotiate");
swarm.dial_alice(addr).await?;
let alice_peer_id = swarm.recv_conn_established().await?;
swarm
.request_amounts(alice_peer_id.clone(), amounts.btc)
.await?;
swarm
.send_message0(alice_peer_id.clone(), state0.next_message(&mut rng))
.await?;
let msg0 = swarm.recv_message0().await?;
let state1 = state0.receive(bitcoin_wallet.as_ref(), msg0).await?;
swarm
.send_message1(alice_peer_id.clone(), state1.next_message())
.await?;
let msg1 = swarm.recv_message1().await?;
let state2 = state1.receive(msg1)?;
swarm
.send_message2(alice_peer_id.clone(), state2.next_message())
.await?;
Ok((state2, alice_peer_id))
}

View File

@ -1,5 +1,5 @@
use crate::{
bob::{event_loop::EventLoopHandle, negotiate::negotiate},
bob::event_loop::EventLoopHandle,
state,
state::{Bob, Swap},
storage::Database,
@ -17,7 +17,7 @@ use std::{convert::TryFrom, fmt, sync::Arc};
use tracing::info;
use uuid::Uuid;
use xmr_btc::{
bob::{self},
bob::{self, State2},
Epoch,
};
@ -393,3 +393,42 @@ where
}
}
}
pub async fn negotiate<R>(
state0: xmr_btc::bob::State0,
amounts: SwapAmounts,
swarm: &mut EventLoopHandle,
addr: Multiaddr,
mut rng: R,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
) -> Result<(State2, PeerId)>
where
R: RngCore + CryptoRng + Send,
{
tracing::trace!("Starting negotiate");
swarm.dial_alice(addr).await?;
let alice_peer_id = swarm.recv_conn_established().await?;
swarm
.request_amounts(alice_peer_id.clone(), amounts.btc)
.await?;
swarm
.send_message0(alice_peer_id.clone(), state0.next_message(&mut rng))
.await?;
let msg0 = swarm.recv_message0().await?;
let state1 = state0.receive(bitcoin_wallet.as_ref(), msg0).await?;
swarm
.send_message1(alice_peer_id.clone(), state1.next_message())
.await?;
let msg1 = swarm.recv_message1().await?;
let state2 = state1.receive(msg1)?;
swarm
.send_message2(alice_peer_id.clone(), state2.next_message())
.await?;
Ok((state2, alice_peer_id))
}