mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-01-23 05:41:07 -05:00
Move Behaviour
in dedicated module
This commit is contained in:
parent
87be9aeb2a
commit
b5b990257a
@ -1,24 +1,13 @@
|
|||||||
//! Run an XMR/BTC swap in the role of Alice.
|
//! Run an XMR/BTC swap in the role of Alice.
|
||||||
//! Alice holds XMR and wishes receive BTC.
|
//! Alice holds XMR and wishes receive BTC.
|
||||||
use crate::{
|
use crate::{
|
||||||
bitcoin, database,
|
bitcoin, database, database::Database, execution_params::ExecutionParams, monero,
|
||||||
database::Database,
|
network::Seed as NetworkSeed, protocol::SwapAmounts, seed::Seed,
|
||||||
execution_params::ExecutionParams,
|
|
||||||
monero,
|
|
||||||
network::{
|
|
||||||
peer_tracker::{self, PeerTracker},
|
|
||||||
Seed as NetworkSeed,
|
|
||||||
},
|
|
||||||
protocol::{bob::EncryptedSignature, SwapAmounts},
|
|
||||||
seed::Seed,
|
|
||||||
};
|
|
||||||
use anyhow::{bail, Error, Result};
|
|
||||||
use libp2p::{
|
|
||||||
core::Multiaddr, identity::Keypair, request_response::ResponseChannel, NetworkBehaviour, PeerId,
|
|
||||||
};
|
};
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use libp2p::{core::Multiaddr, identity::Keypair, PeerId};
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tracing::{debug, info};
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
@ -29,9 +18,9 @@ pub use self::{
|
|||||||
swap_response::*,
|
swap_response::*,
|
||||||
transfer_proof::TransferProof,
|
transfer_proof::TransferProof,
|
||||||
};
|
};
|
||||||
use crate::protocol::bob::SwapRequest;
|
|
||||||
pub use execution_setup::Message3;
|
pub use execution_setup::Message3;
|
||||||
|
|
||||||
|
mod behaviour;
|
||||||
mod encrypted_signature;
|
mod encrypted_signature;
|
||||||
pub mod event_loop;
|
pub mod event_loop;
|
||||||
mod execution_setup;
|
mod execution_setup;
|
||||||
@ -188,109 +177,3 @@ impl Builder {
|
|||||||
Ok(AliceState::Started { amounts, state0 })
|
Ok(AliceState::Started { amounts, state0 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
ConnectionEstablished(PeerId),
|
|
||||||
SwapRequest {
|
|
||||||
msg: SwapRequest,
|
|
||||||
channel: ResponseChannel<SwapResponse>,
|
|
||||||
},
|
|
||||||
ExecutionSetupDone(Result<Box<State3>>),
|
|
||||||
TransferProofAcknowledged,
|
|
||||||
EncryptedSignature {
|
|
||||||
msg: Box<EncryptedSignature>,
|
|
||||||
channel: ResponseChannel<()>,
|
|
||||||
},
|
|
||||||
ResponseSent, // Same variant is used for all messages as no processing is done
|
|
||||||
Failure(Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<peer_tracker::OutEvent> for OutEvent {
|
|
||||||
fn from(event: peer_tracker::OutEvent) -> Self {
|
|
||||||
match event {
|
|
||||||
peer_tracker::OutEvent::ConnectionEstablished(id) => {
|
|
||||||
OutEvent::ConnectionEstablished(id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<swap_response::OutEvent> for OutEvent {
|
|
||||||
fn from(event: swap_response::OutEvent) -> Self {
|
|
||||||
use swap_response::OutEvent::*;
|
|
||||||
match event {
|
|
||||||
MsgReceived { msg, channel } => OutEvent::SwapRequest { msg, channel },
|
|
||||||
ResponseSent => OutEvent::ResponseSent,
|
|
||||||
Failure(err) => OutEvent::Failure(err.context("Swap Request/Response failure")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<execution_setup::OutEvent> for OutEvent {
|
|
||||||
fn from(event: execution_setup::OutEvent) -> Self {
|
|
||||||
match event {
|
|
||||||
execution_setup::OutEvent::Done(res) => OutEvent::ExecutionSetupDone(res.map(Box::new)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<transfer_proof::OutEvent> for OutEvent {
|
|
||||||
fn from(event: transfer_proof::OutEvent) -> Self {
|
|
||||||
use transfer_proof::OutEvent::*;
|
|
||||||
match event {
|
|
||||||
Acknowledged => OutEvent::TransferProofAcknowledged,
|
|
||||||
Failure(err) => OutEvent::Failure(err.context("Failure with Transfer Proof")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<encrypted_signature::OutEvent> for OutEvent {
|
|
||||||
fn from(event: encrypted_signature::OutEvent) -> Self {
|
|
||||||
use encrypted_signature::OutEvent::*;
|
|
||||||
match event {
|
|
||||||
MsgReceived { msg, channel } => OutEvent::EncryptedSignature {
|
|
||||||
msg: Box::new(msg),
|
|
||||||
channel,
|
|
||||||
},
|
|
||||||
AckSent => OutEvent::ResponseSent,
|
|
||||||
Failure(err) => OutEvent::Failure(err.context("Failure with Encrypted Signature")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Alice.
|
|
||||||
#[derive(NetworkBehaviour, Default)]
|
|
||||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour {
|
|
||||||
pt: PeerTracker,
|
|
||||||
amounts: swap_response::Behaviour,
|
|
||||||
execution_setup: execution_setup::Behaviour,
|
|
||||||
transfer_proof: transfer_proof::Behaviour,
|
|
||||||
encrypted_signature: encrypted_signature::Behaviour,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Behaviour {
|
|
||||||
/// Alice always sends her messages as a response to a request from Bob.
|
|
||||||
pub fn send_swap_response(
|
|
||||||
&mut self,
|
|
||||||
channel: ResponseChannel<SwapResponse>,
|
|
||||||
swap_response: SwapResponse,
|
|
||||||
) -> Result<()> {
|
|
||||||
self.amounts.send(channel, swap_response)?;
|
|
||||||
info!("Sent swap response");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn start_execution_setup(&mut self, bob_peer_id: PeerId, state0: State0) {
|
|
||||||
self.execution_setup.run(bob_peer_id, state0);
|
|
||||||
info!("Start execution setup with {}", bob_peer_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Send Transfer Proof to Bob.
|
|
||||||
pub fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) {
|
|
||||||
self.transfer_proof.send(bob, msg);
|
|
||||||
debug!("Sent Transfer Proof");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
124
swap/src/protocol/alice/behaviour.rs
Normal file
124
swap/src/protocol/alice/behaviour.rs
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
use crate::{
|
||||||
|
network::{peer_tracker, peer_tracker::PeerTracker},
|
||||||
|
protocol::{
|
||||||
|
alice,
|
||||||
|
alice::{
|
||||||
|
encrypted_signature, execution_setup, swap_response, transfer_proof, State0, State3,
|
||||||
|
SwapResponse, TransferProof,
|
||||||
|
},
|
||||||
|
bob::{EncryptedSignature, SwapRequest},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use anyhow::{Error, Result};
|
||||||
|
use libp2p::{request_response::ResponseChannel, NetworkBehaviour, PeerId};
|
||||||
|
use tracing::{debug, info};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum OutEvent {
|
||||||
|
ConnectionEstablished(PeerId),
|
||||||
|
SwapRequest {
|
||||||
|
msg: SwapRequest,
|
||||||
|
channel: ResponseChannel<SwapResponse>,
|
||||||
|
},
|
||||||
|
ExecutionSetupDone(anyhow::Result<Box<State3>>),
|
||||||
|
TransferProofAcknowledged,
|
||||||
|
EncryptedSignature {
|
||||||
|
msg: Box<EncryptedSignature>,
|
||||||
|
channel: ResponseChannel<()>,
|
||||||
|
},
|
||||||
|
ResponseSent, // Same variant is used for all messages as no processing is done
|
||||||
|
Failure(Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<peer_tracker::OutEvent> for OutEvent {
|
||||||
|
fn from(event: peer_tracker::OutEvent) -> Self {
|
||||||
|
match event {
|
||||||
|
peer_tracker::OutEvent::ConnectionEstablished(id) => {
|
||||||
|
OutEvent::ConnectionEstablished(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<alice::OutEvent> for OutEvent {
|
||||||
|
fn from(event: alice::OutEvent) -> Self {
|
||||||
|
use crate::protocol::alice::OutEvent::*;
|
||||||
|
match event {
|
||||||
|
MsgReceived { msg, channel } => OutEvent::SwapRequest { msg, channel },
|
||||||
|
ResponseSent => OutEvent::ResponseSent,
|
||||||
|
Failure(err) => OutEvent::Failure(err.context("Swap Request/Response failure")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<execution_setup::OutEvent> for OutEvent {
|
||||||
|
fn from(event: execution_setup::OutEvent) -> Self {
|
||||||
|
match event {
|
||||||
|
execution_setup::OutEvent::Done(res) => OutEvent::ExecutionSetupDone(res.map(Box::new)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<transfer_proof::OutEvent> for OutEvent {
|
||||||
|
fn from(event: transfer_proof::OutEvent) -> Self {
|
||||||
|
use crate::protocol::alice::transfer_proof::OutEvent::*;
|
||||||
|
match event {
|
||||||
|
Acknowledged => OutEvent::TransferProofAcknowledged,
|
||||||
|
Failure(err) => OutEvent::Failure(err.context("Failure with Transfer Proof")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<encrypted_signature::OutEvent> for OutEvent {
|
||||||
|
fn from(event: encrypted_signature::OutEvent) -> Self {
|
||||||
|
use crate::protocol::alice::encrypted_signature::OutEvent::*;
|
||||||
|
match event {
|
||||||
|
MsgReceived { msg, channel } => OutEvent::EncryptedSignature {
|
||||||
|
msg: Box::new(msg),
|
||||||
|
channel,
|
||||||
|
},
|
||||||
|
AckSent => OutEvent::ResponseSent,
|
||||||
|
Failure(err) => OutEvent::Failure(err.context("Failure with Encrypted Signature")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Alice.
|
||||||
|
#[derive(NetworkBehaviour, Default)]
|
||||||
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct Behaviour {
|
||||||
|
pt: PeerTracker,
|
||||||
|
swap_response: swap_response::Behaviour,
|
||||||
|
execution_setup: execution_setup::Behaviour,
|
||||||
|
transfer_proof: transfer_proof::Behaviour,
|
||||||
|
encrypted_signature: encrypted_signature::Behaviour,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Behaviour {
|
||||||
|
/// Alice always sends her messages as a response to a request from Bob.
|
||||||
|
pub fn send_swap_response(
|
||||||
|
&mut self,
|
||||||
|
channel: ResponseChannel<SwapResponse>,
|
||||||
|
swap_response: SwapResponse,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
self.swap_response.send(channel, swap_response)?;
|
||||||
|
info!("Sent swap response");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_execution_setup(&mut self, bob_peer_id: PeerId, state0: State0) {
|
||||||
|
self.execution_setup.run(bob_peer_id, state0);
|
||||||
|
info!("Start execution setup with {}", bob_peer_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send Transfer Proof to Bob.
|
||||||
|
pub fn send_transfer_proof(&mut self, bob: PeerId, msg: TransferProof) {
|
||||||
|
self.transfer_proof.send(bob, msg);
|
||||||
|
debug!("Sent Transfer Proof");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send_encrypted_signature_ack(&mut self, channel: ResponseChannel<()>) -> Result<()> {
|
||||||
|
self.encrypted_signature.send_ack(channel)
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,10 @@ use crate::{
|
|||||||
execution_params::ExecutionParams,
|
execution_params::ExecutionParams,
|
||||||
network::{transport, TokioExecutor},
|
network::{transport, TokioExecutor},
|
||||||
protocol::{
|
protocol::{
|
||||||
alice::{Behaviour, OutEvent, State0, State3, SwapResponse, TransferProof},
|
alice::{
|
||||||
|
behaviour::{Behaviour, OutEvent},
|
||||||
|
State0, State3, SwapResponse, TransferProof,
|
||||||
|
},
|
||||||
bob::{EncryptedSignature, SwapRequest},
|
bob::{EncryptedSignature, SwapRequest},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -207,7 +210,7 @@ impl EventLoop {
|
|||||||
OutEvent::EncryptedSignature{ msg, channel } => {
|
OutEvent::EncryptedSignature{ msg, channel } => {
|
||||||
let _ = self.recv_encrypted_signature.send(*msg).await;
|
let _ = self.recv_encrypted_signature.send(*msg).await;
|
||||||
// Send back empty response so that the request/response protocol completes.
|
// Send back empty response so that the request/response protocol completes.
|
||||||
if let Err(error) = self.swarm.encrypted_signature.send_ack(channel) {
|
if let Err(error) = self.swarm.send_encrypted_signature_ack(channel) {
|
||||||
error!("Failed to send Encrypted Signature ack: {:?}", error);
|
error!("Failed to send Encrypted Signature ack: {:?}", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user