mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-15 01:36:00 -04:00
Make shit compile
This commit is contained in:
parent
d143e108fc
commit
aa35c5d8ce
8 changed files with 342 additions and 848 deletions
|
@ -144,13 +144,9 @@ async fn main() -> Result<()> {
|
|||
}
|
||||
};
|
||||
|
||||
let current_balance = monero_wallet.get_balance().await?;
|
||||
let lock_fee = monero_wallet.static_tx_fee_estimate();
|
||||
let kraken_rate = KrakenRate::new(config.maker.ask_spread, kraken_price_updates);
|
||||
let mut swarm = swarm::asb(
|
||||
&seed,
|
||||
current_balance,
|
||||
lock_fee,
|
||||
config.maker.min_buy_btc,
|
||||
config.maker.max_buy_btc,
|
||||
kraken_rate.clone(),
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::protocol::alice::event_loop::LatestRate;
|
||||
use crate::protocol::{alice, bob};
|
||||
use crate::seed::Seed;
|
||||
use crate::{asb, cli, env, monero, tor};
|
||||
use crate::{asb, cli, env, tor};
|
||||
use anyhow::Result;
|
||||
use libp2p::swarm::SwarmBuilder;
|
||||
use libp2p::{PeerId, Swarm};
|
||||
|
@ -10,8 +10,6 @@ use std::fmt::Debug;
|
|||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn asb<LR>(
|
||||
seed: &Seed,
|
||||
balance: monero::Amount,
|
||||
lock_fee: monero::Amount,
|
||||
min_buy: bitcoin::Amount,
|
||||
max_buy: bitcoin::Amount,
|
||||
latest_rate: LR,
|
||||
|
@ -19,17 +17,9 @@ pub fn asb<LR>(
|
|||
env_config: env::Config,
|
||||
) -> Result<Swarm<alice::Behaviour<LR>>>
|
||||
where
|
||||
LR: LatestRate + Send + 'static + Debug,
|
||||
LR: LatestRate + Send + 'static + Debug + Clone,
|
||||
{
|
||||
let behaviour = alice::Behaviour::new(
|
||||
balance,
|
||||
lock_fee,
|
||||
min_buy,
|
||||
max_buy,
|
||||
latest_rate,
|
||||
resume_only,
|
||||
env_config,
|
||||
);
|
||||
let behaviour = alice::Behaviour::new(min_buy, max_buy, latest_rate, resume_only, env_config);
|
||||
|
||||
let identity = seed.derive_libp2p_identity();
|
||||
let transport = asb::transport::new(&identity)?;
|
||||
|
|
|
@ -21,7 +21,6 @@ pub use self::swap::{run, run_until};
|
|||
|
||||
mod behaviour;
|
||||
pub mod event_loop;
|
||||
mod execution_setup;
|
||||
mod recovery;
|
||||
pub mod state;
|
||||
pub mod swap;
|
||||
|
|
|
@ -4,18 +4,17 @@ use libp2p::request_response::{RequestId, ResponseChannel};
|
|||
use libp2p::{NetworkBehaviour, PeerId};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::env;
|
||||
use crate::network::quote::BidQuote;
|
||||
use crate::network::{encrypted_signature, quote, transfer_proof};
|
||||
use crate::protocol::alice::event_loop::LatestRate;
|
||||
use crate::protocol::alice::swap_setup::WalletSnapshot;
|
||||
use crate::protocol::alice::{execution_setup, swap_setup, State3};
|
||||
use crate::{env, monero};
|
||||
use tokio::sync::oneshot;
|
||||
use crate::protocol::alice::{swap_setup, State3};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum OutEvent {
|
||||
SwapSetupInitiated {
|
||||
send_wallet_snapshot: oneshot::Sender<WalletSnapshot>,
|
||||
send_wallet_snapshot: bmrng::RequestReceiver<bitcoin::Amount, WalletSnapshot>,
|
||||
},
|
||||
SwapSetupCompleted {
|
||||
peer_id: PeerId,
|
||||
|
@ -73,8 +72,7 @@ where
|
|||
LR: LatestRate + Send + 'static,
|
||||
{
|
||||
pub quote: quote::Behaviour,
|
||||
pub spot_price: swap_setup::Behaviour<LR>,
|
||||
pub execution_setup: execution_setup::Behaviour,
|
||||
pub swap_setup: swap_setup::Behaviour<LR>,
|
||||
pub transfer_proof: transfer_proof::Behaviour,
|
||||
pub encrypted_signature: encrypted_signature::Behaviour,
|
||||
|
||||
|
@ -89,8 +87,6 @@ where
|
|||
LR: LatestRate + Send + 'static,
|
||||
{
|
||||
pub fn new(
|
||||
balance: monero::Amount,
|
||||
lock_fee: monero::Amount,
|
||||
min_buy: bitcoin::Amount,
|
||||
max_buy: bitcoin::Amount,
|
||||
latest_rate: LR,
|
||||
|
@ -99,16 +95,13 @@ where
|
|||
) -> Self {
|
||||
Self {
|
||||
quote: quote::alice(),
|
||||
spot_price: swap_setup::Behaviour::new(
|
||||
balance,
|
||||
lock_fee,
|
||||
swap_setup: swap_setup::Behaviour::new(
|
||||
min_buy,
|
||||
max_buy,
|
||||
env_config,
|
||||
latest_rate,
|
||||
resume_only,
|
||||
),
|
||||
execution_setup: Default::default(),
|
||||
transfer_proof: transfer_proof::alice(),
|
||||
encrypted_signature: encrypted_signature::alice(),
|
||||
ping: Ping::default(),
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::env::Config;
|
|||
use crate::network::quote::BidQuote;
|
||||
use crate::network::transfer_proof;
|
||||
use crate::protocol::alice::swap_setup::WalletSnapshot;
|
||||
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State0, State3, Swap};
|
||||
use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State3, Swap};
|
||||
use crate::{bitcoin, kraken, monero};
|
||||
use anyhow::{Context, Result};
|
||||
use futures::future;
|
||||
|
@ -13,7 +13,6 @@ use futures::stream::{FuturesUnordered, StreamExt};
|
|||
use libp2p::request_response::{RequestId, ResponseChannel};
|
||||
use libp2p::swarm::SwarmEvent;
|
||||
use libp2p::{PeerId, Swarm};
|
||||
use rand::rngs::OsRng;
|
||||
use rust_decimal::Decimal;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::Infallible;
|
||||
|
@ -35,7 +34,7 @@ type OutgoingTransferProof =
|
|||
#[allow(missing_debug_implementations)]
|
||||
pub struct EventLoop<LR>
|
||||
where
|
||||
LR: LatestRate + Send + 'static + Debug,
|
||||
LR: LatestRate + Send + 'static + Debug + Clone,
|
||||
{
|
||||
swarm: libp2p::Swarm<Behaviour<LR>>,
|
||||
env_config: Config,
|
||||
|
@ -65,7 +64,7 @@ where
|
|||
|
||||
impl<LR> EventLoop<LR>
|
||||
where
|
||||
LR: LatestRate + Send + 'static + Debug,
|
||||
LR: LatestRate + Send + 'static + Debug + Clone,
|
||||
{
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
|
@ -151,21 +150,23 @@ where
|
|||
tokio::select! {
|
||||
swarm_event = self.swarm.next_event() => {
|
||||
match swarm_event {
|
||||
SwarmEvent::Behaviour(OutEvent::SwapSetupInitiated { send_wallet_snapshot }) => {
|
||||
SwarmEvent::Behaviour(OutEvent::SwapSetupInitiated { mut send_wallet_snapshot }) => {
|
||||
|
||||
let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet).await {
|
||||
let (btc, responder) = send_wallet_snapshot.recv().await.expect("TODO: handle error");
|
||||
|
||||
let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet, btc).await {
|
||||
Ok(wallet_snapshot) => wallet_snapshot,
|
||||
Err(error) => {
|
||||
tracing::error!("Swap request will be ignored because we were unable to create wallet snapshot for swap: {:#}", error)
|
||||
tracing::error!("Swap request will be ignored because we were unable to create wallet snapshot for swap: {:#}", error);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
match send_wallet_snapshot.send().await {
|
||||
Ok()
|
||||
}
|
||||
// Ignore result, we should never hit this because the receiver will alive as long as the connection is.
|
||||
let _ = responder.respond(wallet_snapshot);
|
||||
}
|
||||
SwarmEvent::Behaviour(OutEvent::SwapSetupCompleted{peer_id, swap_id, state3}) => {
|
||||
let _ = self.handle_execution_setup_done(bob_peer_id, swap_id, *state3).await;
|
||||
let _ = self.handle_execution_setup_done(peer_id, swap_id, *state3).await;
|
||||
}
|
||||
SwarmEvent::Behaviour(OutEvent::SwapDeclined { peer, error }) => {
|
||||
tracing::warn!(%peer, "Ignoring spot price request because: {}", error);
|
||||
|
@ -175,7 +176,8 @@ where
|
|||
let current_balance = self.monero_wallet.get_balance().await;
|
||||
match current_balance {
|
||||
Ok(balance) => {
|
||||
self.swarm.behaviour_mut().spot_price.update_balance(balance);
|
||||
|
||||
// FIXME self.swarm.behaviour_mut().spot_price.update_balance(balance);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to fetch Monero balance: {:#}", e);
|
||||
|
|
|
@ -1,76 +0,0 @@
|
|||
use crate::network::cbor_request_response::BUF_SIZE;
|
||||
use crate::protocol::alice::{State0, State3};
|
||||
use crate::protocol::{alice, Message0, Message2, Message4};
|
||||
use anyhow::{Context, Error};
|
||||
use libp2p::PeerId;
|
||||
use libp2p_async_await::BehaviourOutEvent;
|
||||
use std::time::Duration;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum OutEvent {
|
||||
Done {
|
||||
bob_peer_id: PeerId,
|
||||
swap_id: Uuid,
|
||||
state3: State3,
|
||||
},
|
||||
Failure {
|
||||
peer: PeerId,
|
||||
error: Error,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<BehaviourOutEvent<(PeerId, (Uuid, State3)), (), Error>> for OutEvent {
|
||||
fn from(event: BehaviourOutEvent<(PeerId, (Uuid, State3)), (), Error>) -> Self {
|
||||
match event {
|
||||
BehaviourOutEvent::Inbound(_, Ok((bob_peer_id, (swap_id, state3)))) => OutEvent::Done {
|
||||
bob_peer_id,
|
||||
swap_id,
|
||||
state3,
|
||||
},
|
||||
BehaviourOutEvent::Inbound(peer, Err(e)) => OutEvent::Failure { peer, error: e },
|
||||
BehaviourOutEvent::Outbound(..) => unreachable!("Alice only supports inbound"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(libp2p::NetworkBehaviour)]
|
||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||
pub struct Behaviour {
|
||||
inner: libp2p_async_await::Behaviour<(PeerId, (Uuid, State3)), (), anyhow::Error>,
|
||||
}
|
||||
|
||||
impl Default for Behaviour {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inner: libp2p_async_await::Behaviour::new(b"/comit/xmr/btc/execution_setup/1.0.0"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Behaviour {
|
||||
pub fn run(&mut self, bob: PeerId, state0: State0) {
|
||||
self.inner.do_protocol_listener(bob, move |mut substream| {
|
||||
let protocol = async move { Ok((bob, (swap_id, state3))) };
|
||||
|
||||
async move { tokio::time::timeout(Duration::from_secs(60), protocol).await? }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OutEvent> for alice::OutEvent {
|
||||
fn from(event: OutEvent) -> Self {
|
||||
match event {
|
||||
OutEvent::Done {
|
||||
bob_peer_id,
|
||||
state3,
|
||||
swap_id,
|
||||
} => Self::SwapSetupCompleted {
|
||||
peer_id: bob_peer_id,
|
||||
state3: Box::new(state3),
|
||||
swap_id,
|
||||
},
|
||||
OutEvent::Failure { peer, error } => Self::Failure { peer, error },
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -224,8 +224,6 @@ async fn start_alice(
|
|||
) -> (AliceApplicationHandle, Receiver<alice::Swap>) {
|
||||
let db = Arc::new(Database::open(db_path.as_path()).unwrap());
|
||||
|
||||
let current_balance = monero_wallet.get_balance().await.unwrap();
|
||||
let lock_fee = monero_wallet.static_tx_fee_estimate();
|
||||
let min_buy = bitcoin::Amount::from_sat(u64::MIN);
|
||||
let max_buy = bitcoin::Amount::from_sat(u64::MAX);
|
||||
let latest_rate = FixedRate::default();
|
||||
|
@ -233,8 +231,6 @@ async fn start_alice(
|
|||
|
||||
let mut swarm = swarm::asb(
|
||||
&seed,
|
||||
current_balance,
|
||||
lock_fee,
|
||||
min_buy,
|
||||
max_buy,
|
||||
latest_rate,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue