mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-15 09:46:14 -04:00
WIP - complete behaviour API and ASB event loop
This commit is contained in:
parent
a3e6f75eb1
commit
d143e108fc
5 changed files with 24 additions and 56 deletions
|
@ -4,18 +4,18 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{bitcoin, monero};
|
|
||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
use crate::env::Config;
|
use crate::env::Config;
|
||||||
|
use crate::{bitcoin, monero};
|
||||||
|
|
||||||
pub use self::behaviour::{Behaviour, OutEvent};
|
pub use self::behaviour::{Behaviour, OutEvent};
|
||||||
pub use self::event_loop::{EventLoop, EventLoopHandle};
|
pub use self::event_loop::{EventLoop, EventLoopHandle};
|
||||||
pub use self::recovery::{cancel, punish, redeem, refund, safely_abort};
|
|
||||||
pub use self::recovery::cancel::cancel;
|
pub use self::recovery::cancel::cancel;
|
||||||
pub use self::recovery::punish::punish;
|
pub use self::recovery::punish::punish;
|
||||||
pub use self::recovery::redeem::redeem;
|
pub use self::recovery::redeem::redeem;
|
||||||
pub use self::recovery::refund::refund;
|
pub use self::recovery::refund::refund;
|
||||||
pub use self::recovery::safely_abort::safely_abort;
|
pub use self::recovery::safely_abort::safely_abort;
|
||||||
|
pub use self::recovery::{cancel, punish, redeem, refund, safely_abort};
|
||||||
pub use self::state::*;
|
pub use self::state::*;
|
||||||
pub use self::swap::{run, run_until};
|
pub use self::swap::{run, run_until};
|
||||||
|
|
||||||
|
|
|
@ -1,35 +1,35 @@
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::{anyhow, Error};
|
||||||
use libp2p::{NetworkBehaviour, PeerId};
|
|
||||||
use libp2p::ping::{Ping, PingEvent};
|
use libp2p::ping::{Ping, PingEvent};
|
||||||
use libp2p::request_response::{RequestId, ResponseChannel};
|
use libp2p::request_response::{RequestId, ResponseChannel};
|
||||||
|
use libp2p::{NetworkBehaviour, PeerId};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{env, monero};
|
|
||||||
use crate::network::{encrypted_signature, quote, transfer_proof};
|
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::protocol::alice::{execution_setup, State3, swap_setup};
|
use crate::network::{encrypted_signature, quote, transfer_proof};
|
||||||
use crate::protocol::alice::event_loop::LatestRate;
|
use crate::protocol::alice::event_loop::LatestRate;
|
||||||
use crate::protocol::alice::swap_setup::WalletSnapshot;
|
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 tokio::sync::oneshot;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OutEvent {
|
pub enum OutEvent {
|
||||||
SwapRequestDeclined {
|
SwapSetupInitiated {
|
||||||
|
send_wallet_snapshot: oneshot::Sender<WalletSnapshot>,
|
||||||
|
},
|
||||||
|
SwapSetupCompleted {
|
||||||
|
peer_id: PeerId,
|
||||||
|
swap_id: Uuid,
|
||||||
|
state3: Box<State3>,
|
||||||
|
},
|
||||||
|
SwapDeclined {
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
error: swap_setup::Error,
|
error: swap_setup::Error,
|
||||||
},
|
},
|
||||||
SwapInitiated {
|
|
||||||
send_wallet_snapshot: oneshot::Sender<WalletSnapshot>
|
|
||||||
},
|
|
||||||
QuoteRequested {
|
QuoteRequested {
|
||||||
channel: ResponseChannel<BidQuote>,
|
channel: ResponseChannel<BidQuote>,
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
},
|
},
|
||||||
ExecutionSetupDone {
|
|
||||||
bob_peer_id: PeerId,
|
|
||||||
swap_id: Uuid,
|
|
||||||
state3: Box<State3>,
|
|
||||||
},
|
|
||||||
TransferProofAcknowledged {
|
TransferProofAcknowledged {
|
||||||
peer: PeerId,
|
peer: PeerId,
|
||||||
id: RequestId,
|
id: RequestId,
|
||||||
|
|
|
@ -3,6 +3,7 @@ use crate::database::Database;
|
||||||
use crate::env::Config;
|
use crate::env::Config;
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
use crate::network::transfer_proof;
|
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, State0, State3, Swap};
|
||||||
use crate::{bitcoin, kraken, monero};
|
use crate::{bitcoin, kraken, monero};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
@ -20,7 +21,6 @@ use std::fmt::Debug;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use crate::protocol::alice::swap_setup::WalletSnapshot;
|
|
||||||
|
|
||||||
/// A future that resolves to a tuple of `PeerId`, `transfer_proof::Request` and
|
/// A future that resolves to a tuple of `PeerId`, `transfer_proof::Request` and
|
||||||
/// `Responder`.
|
/// `Responder`.
|
||||||
|
@ -151,7 +151,7 @@ where
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
swarm_event = self.swarm.next_event() => {
|
swarm_event = self.swarm.next_event() => {
|
||||||
match swarm_event {
|
match swarm_event {
|
||||||
SwarmEvent::Behaviour(OutEvent::SwapInitiated { send_wallet_snapshot }) => {
|
SwarmEvent::Behaviour(OutEvent::SwapSetupInitiated { send_wallet_snapshot }) => {
|
||||||
|
|
||||||
let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet).await {
|
let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet).await {
|
||||||
Ok(wallet_snapshot) => wallet_snapshot,
|
Ok(wallet_snapshot) => wallet_snapshot,
|
||||||
|
@ -163,28 +163,11 @@ where
|
||||||
match send_wallet_snapshot.send().await {
|
match send_wallet_snapshot.send().await {
|
||||||
Ok()
|
Ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move into execution setup as part of swap_setup
|
|
||||||
// let state0 = match State0::new(
|
|
||||||
// btc,
|
|
||||||
// xmr,
|
|
||||||
// self.env_config,
|
|
||||||
// redeem_address,
|
|
||||||
// punish_address,
|
|
||||||
// tx_redeem_fee,
|
|
||||||
// tx_punish_fee,
|
|
||||||
// &mut OsRng
|
|
||||||
// ) {
|
|
||||||
// Ok(state) => state,
|
|
||||||
// Err(error) => {
|
|
||||||
// tracing::warn!(%peer, "Failed to make State0 for execution setup. Error {:#}", error);
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// self.swarm.behaviour_mut().execution_setup.run(peer, state0);
|
|
||||||
}
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::SwapRequestDeclined { peer, error }) => {
|
SwarmEvent::Behaviour(OutEvent::SwapSetupCompleted{peer_id, swap_id, state3}) => {
|
||||||
|
let _ = self.handle_execution_setup_done(bob_peer_id, swap_id, *state3).await;
|
||||||
|
}
|
||||||
|
SwarmEvent::Behaviour(OutEvent::SwapDeclined { peer, error }) => {
|
||||||
tracing::warn!(%peer, "Ignoring spot price request because: {}", error);
|
tracing::warn!(%peer, "Ignoring spot price request because: {}", error);
|
||||||
}
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::QuoteRequested { channel, peer }) => {
|
SwarmEvent::Behaviour(OutEvent::QuoteRequested { channel, peer }) => {
|
||||||
|
@ -211,9 +194,6 @@ where
|
||||||
tracing::debug!(%peer, "Failed to respond with quote");
|
tracing::debug!(%peer, "Failed to respond with quote");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SwarmEvent::Behaviour(OutEvent::ExecutionSetupDone{bob_peer_id, swap_id, state3}) => {
|
|
||||||
let _ = self.handle_execution_setup_done(bob_peer_id, swap_id, *state3).await;
|
|
||||||
}
|
|
||||||
SwarmEvent::Behaviour(OutEvent::TransferProofAcknowledged { peer, id }) => {
|
SwarmEvent::Behaviour(OutEvent::TransferProofAcknowledged { peer, id }) => {
|
||||||
tracing::debug!(%peer, "Bob acknowledged transfer proof");
|
tracing::debug!(%peer, "Bob acknowledged transfer proof");
|
||||||
if let Some(responder) = self.inflight_transfer_proofs.remove(&id) {
|
if let Some(responder) = self.inflight_transfer_proofs.remove(&id) {
|
||||||
|
|
|
@ -51,11 +51,7 @@ impl Default for Behaviour {
|
||||||
impl Behaviour {
|
impl Behaviour {
|
||||||
pub fn run(&mut self, bob: PeerId, state0: State0) {
|
pub fn run(&mut self, bob: PeerId, state0: State0) {
|
||||||
self.inner.do_protocol_listener(bob, move |mut substream| {
|
self.inner.do_protocol_listener(bob, move |mut substream| {
|
||||||
let protocol = async move {
|
let protocol = async move { Ok((bob, (swap_id, state3))) };
|
||||||
|
|
||||||
|
|
||||||
Ok((bob, (swap_id, state3)))
|
|
||||||
};
|
|
||||||
|
|
||||||
async move { tokio::time::timeout(Duration::from_secs(60), protocol).await? }
|
async move { tokio::time::timeout(Duration::from_secs(60), protocol).await? }
|
||||||
});
|
});
|
||||||
|
@ -69,8 +65,8 @@ impl From<OutEvent> for alice::OutEvent {
|
||||||
bob_peer_id,
|
bob_peer_id,
|
||||||
state3,
|
state3,
|
||||||
swap_id,
|
swap_id,
|
||||||
} => Self::ExecutionSetupDone {
|
} => Self::SwapSetupCompleted {
|
||||||
bob_peer_id,
|
peer_id: bob_peer_id,
|
||||||
state3: Box::new(state3),
|
state3: Box::new(state3),
|
||||||
swap_id,
|
swap_id,
|
||||||
},
|
},
|
||||||
|
|
|
@ -91,8 +91,6 @@ where
|
||||||
LR: LatestRate + Send + 'static,
|
LR: LatestRate + Send + 'static,
|
||||||
{
|
{
|
||||||
pub fn new(
|
pub fn new(
|
||||||
balance: monero::Amount,
|
|
||||||
lock_fee: monero::Amount,
|
|
||||||
min_buy: bitcoin::Amount,
|
min_buy: bitcoin::Amount,
|
||||||
max_buy: bitcoin::Amount,
|
max_buy: bitcoin::Amount,
|
||||||
env_config: env::Config,
|
env_config: env::Config,
|
||||||
|
@ -101,8 +99,6 @@ where
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
events: Default::default(),
|
events: Default::default(),
|
||||||
balance,
|
|
||||||
lock_fee,
|
|
||||||
min_buy,
|
min_buy,
|
||||||
max_buy,
|
max_buy,
|
||||||
env_config,
|
env_config,
|
||||||
|
@ -110,10 +106,6 @@ where
|
||||||
resume_only,
|
resume_only,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, monero_balance: monero::Amount, redeem_address: bitcoin::Address, punish_address: bitcoin::Address) {
|
|
||||||
self.balance = monero_balance;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<LR> NetworkBehaviour for Behaviour<LR> {
|
impl<LR> NetworkBehaviour for Behaviour<LR> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue