mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-13 08:29:35 -05:00
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:
parent
19066200d1
commit
71cf501c29
@ -24,7 +24,6 @@ mod message0;
|
|||||||
mod message1;
|
mod message1;
|
||||||
mod message2;
|
mod message2;
|
||||||
mod message3;
|
mod message3;
|
||||||
mod negotiate;
|
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
|
|
||||||
pub type Swarm = libp2p::Swarm<Behaviour>;
|
pub type Swarm = libp2p::Swarm<Behaviour>;
|
||||||
|
@ -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))
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
bob::{event_loop::EventLoopHandle, negotiate::negotiate},
|
bob::event_loop::EventLoopHandle,
|
||||||
state,
|
state,
|
||||||
state::{Bob, Swap},
|
state::{Bob, Swap},
|
||||||
storage::Database,
|
storage::Database,
|
||||||
@ -17,7 +17,7 @@ use std::{convert::TryFrom, fmt, sync::Arc};
|
|||||||
use tracing::info;
|
use tracing::info;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use xmr_btc::{
|
use xmr_btc::{
|
||||||
bob::{self},
|
bob::{self, State2},
|
||||||
Epoch,
|
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))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user