mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-02-02 10:35:22 -05:00
Merge transport and behaviour module into network
This commit is contained in:
parent
ff8cca2e27
commit
e163942850
@ -1,14 +1,14 @@
|
|||||||
mod behaviour;
|
|
||||||
pub mod command;
|
pub mod command;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod event_loop;
|
mod event_loop;
|
||||||
|
mod network;
|
||||||
mod rate;
|
mod rate;
|
||||||
mod recovery;
|
mod recovery;
|
||||||
pub mod tracing;
|
pub mod tracing;
|
||||||
pub mod transport;
|
|
||||||
|
|
||||||
pub use behaviour::{Behaviour, OutEvent};
|
|
||||||
pub use event_loop::{EventLoop, EventLoopHandle, FixedRate, KrakenRate, LatestRate};
|
pub use event_loop::{EventLoop, EventLoopHandle, FixedRate, KrakenRate, LatestRate};
|
||||||
|
pub use network::behaviour::{Behaviour, OutEvent};
|
||||||
|
pub use network::transport;
|
||||||
pub use rate::Rate;
|
pub use rate::Rate;
|
||||||
pub use recovery::cancel::cancel;
|
pub use recovery::cancel::cancel;
|
||||||
pub use recovery::punish::punish;
|
pub use recovery::punish::punish;
|
||||||
|
@ -1,116 +0,0 @@
|
|||||||
use crate::asb::event_loop::LatestRate;
|
|
||||||
use crate::env;
|
|
||||||
use crate::network::quote::BidQuote;
|
|
||||||
use crate::network::swap_setup::alice;
|
|
||||||
use crate::network::swap_setup::alice::WalletSnapshot;
|
|
||||||
use crate::network::{encrypted_signature, quote, transfer_proof};
|
|
||||||
use crate::protocol::alice::State3;
|
|
||||||
use anyhow::{anyhow, Error};
|
|
||||||
use libp2p::ping::{Ping, PingEvent};
|
|
||||||
use libp2p::request_response::{RequestId, ResponseChannel};
|
|
||||||
use libp2p::{NetworkBehaviour, PeerId};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum OutEvent {
|
|
||||||
SwapSetupInitiated {
|
|
||||||
send_wallet_snapshot: bmrng::RequestReceiver<bitcoin::Amount, WalletSnapshot>,
|
|
||||||
},
|
|
||||||
SwapSetupCompleted {
|
|
||||||
peer_id: PeerId,
|
|
||||||
swap_id: Uuid,
|
|
||||||
state3: Box<State3>,
|
|
||||||
},
|
|
||||||
SwapDeclined {
|
|
||||||
peer: PeerId,
|
|
||||||
error: alice::Error,
|
|
||||||
},
|
|
||||||
QuoteRequested {
|
|
||||||
channel: ResponseChannel<BidQuote>,
|
|
||||||
peer: PeerId,
|
|
||||||
},
|
|
||||||
TransferProofAcknowledged {
|
|
||||||
peer: PeerId,
|
|
||||||
id: RequestId,
|
|
||||||
},
|
|
||||||
EncryptedSignatureReceived {
|
|
||||||
msg: Box<encrypted_signature::Request>,
|
|
||||||
channel: ResponseChannel<()>,
|
|
||||||
peer: PeerId,
|
|
||||||
},
|
|
||||||
Failure {
|
|
||||||
peer: PeerId,
|
|
||||||
error: Error,
|
|
||||||
},
|
|
||||||
/// "Fallback" variant that allows the event mapping code to swallow certain
|
|
||||||
/// events that we don't want the caller to deal with.
|
|
||||||
Other,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OutEvent {
|
|
||||||
pub fn unexpected_request(peer: PeerId) -> OutEvent {
|
|
||||||
OutEvent::Failure {
|
|
||||||
peer,
|
|
||||||
error: anyhow!("Unexpected request received"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unexpected_response(peer: PeerId) -> OutEvent {
|
|
||||||
OutEvent::Failure {
|
|
||||||
peer,
|
|
||||||
error: anyhow!("Unexpected response received"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Alice.
|
|
||||||
#[derive(NetworkBehaviour)]
|
|
||||||
#[behaviour(out_event = "OutEvent", event_process = false)]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct Behaviour<LR>
|
|
||||||
where
|
|
||||||
LR: LatestRate + Send + 'static,
|
|
||||||
{
|
|
||||||
pub quote: quote::Behaviour,
|
|
||||||
pub swap_setup: alice::Behaviour<LR>,
|
|
||||||
pub transfer_proof: transfer_proof::Behaviour,
|
|
||||||
pub encrypted_signature: encrypted_signature::Behaviour,
|
|
||||||
|
|
||||||
/// Ping behaviour that ensures that the underlying network connection is
|
|
||||||
/// still alive. If the ping fails a connection close event will be
|
|
||||||
/// emitted that is picked up as swarm event.
|
|
||||||
ping: Ping,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<LR> Behaviour<LR>
|
|
||||||
where
|
|
||||||
LR: LatestRate + Send + 'static,
|
|
||||||
{
|
|
||||||
pub fn new(
|
|
||||||
min_buy: bitcoin::Amount,
|
|
||||||
max_buy: bitcoin::Amount,
|
|
||||||
latest_rate: LR,
|
|
||||||
resume_only: bool,
|
|
||||||
env_config: env::Config,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
quote: quote::asb(),
|
|
||||||
swap_setup: alice::Behaviour::new(
|
|
||||||
min_buy,
|
|
||||||
max_buy,
|
|
||||||
env_config,
|
|
||||||
latest_rate,
|
|
||||||
resume_only,
|
|
||||||
),
|
|
||||||
transfer_proof: transfer_proof::alice(),
|
|
||||||
encrypted_signature: encrypted_signature::alice(),
|
|
||||||
ping: Ping::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<PingEvent> for OutEvent {
|
|
||||||
fn from(_: PingEvent) -> Self {
|
|
||||||
OutEvent::Other
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
use crate::asb::behaviour::{Behaviour, OutEvent};
|
use crate::asb::{Behaviour, OutEvent, Rate};
|
||||||
use crate::asb::Rate;
|
|
||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
use crate::env::Config;
|
use crate::env::Config;
|
||||||
use crate::network::quote::BidQuote;
|
use crate::network::quote::BidQuote;
|
||||||
|
140
swap/src/asb/network.rs
Normal file
140
swap/src/asb/network.rs
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
pub mod transport {
|
||||||
|
use crate::network::transport::authenticate_and_multiplex;
|
||||||
|
use anyhow::Result;
|
||||||
|
use libp2p::core::muxing::StreamMuxerBox;
|
||||||
|
use libp2p::core::transport::Boxed;
|
||||||
|
use libp2p::dns::TokioDnsConfig;
|
||||||
|
use libp2p::tcp::TokioTcpConfig;
|
||||||
|
use libp2p::websocket::WsConfig;
|
||||||
|
use libp2p::{identity, PeerId, Transport};
|
||||||
|
|
||||||
|
/// Creates the libp2p transport for the ASB.
|
||||||
|
pub fn new(identity: &identity::Keypair) -> Result<Boxed<(PeerId, StreamMuxerBox)>> {
|
||||||
|
let tcp = TokioTcpConfig::new().nodelay(true);
|
||||||
|
let tcp_with_dns = TokioDnsConfig::system(tcp)?;
|
||||||
|
let websocket_with_dns = WsConfig::new(tcp_with_dns.clone());
|
||||||
|
|
||||||
|
let transport = tcp_with_dns.or_transport(websocket_with_dns).boxed();
|
||||||
|
|
||||||
|
authenticate_and_multiplex(transport, identity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod behaviour {
|
||||||
|
use crate::asb::event_loop::LatestRate;
|
||||||
|
use crate::env;
|
||||||
|
use crate::network::quote::BidQuote;
|
||||||
|
use crate::network::swap_setup::alice;
|
||||||
|
use crate::network::swap_setup::alice::WalletSnapshot;
|
||||||
|
use crate::network::{encrypted_signature, quote, transfer_proof};
|
||||||
|
use crate::protocol::alice::State3;
|
||||||
|
use anyhow::{anyhow, Error};
|
||||||
|
use libp2p::ping::{Ping, PingEvent};
|
||||||
|
use libp2p::request_response::{RequestId, ResponseChannel};
|
||||||
|
use libp2p::{NetworkBehaviour, PeerId};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum OutEvent {
|
||||||
|
SwapSetupInitiated {
|
||||||
|
send_wallet_snapshot: bmrng::RequestReceiver<bitcoin::Amount, WalletSnapshot>,
|
||||||
|
},
|
||||||
|
SwapSetupCompleted {
|
||||||
|
peer_id: PeerId,
|
||||||
|
swap_id: Uuid,
|
||||||
|
state3: Box<State3>,
|
||||||
|
},
|
||||||
|
SwapDeclined {
|
||||||
|
peer: PeerId,
|
||||||
|
error: alice::Error,
|
||||||
|
},
|
||||||
|
QuoteRequested {
|
||||||
|
channel: ResponseChannel<BidQuote>,
|
||||||
|
peer: PeerId,
|
||||||
|
},
|
||||||
|
TransferProofAcknowledged {
|
||||||
|
peer: PeerId,
|
||||||
|
id: RequestId,
|
||||||
|
},
|
||||||
|
EncryptedSignatureReceived {
|
||||||
|
msg: Box<encrypted_signature::Request>,
|
||||||
|
channel: ResponseChannel<()>,
|
||||||
|
peer: PeerId,
|
||||||
|
},
|
||||||
|
Failure {
|
||||||
|
peer: PeerId,
|
||||||
|
error: Error,
|
||||||
|
},
|
||||||
|
/// "Fallback" variant that allows the event mapping code to swallow
|
||||||
|
/// certain events that we don't want the caller to deal with.
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutEvent {
|
||||||
|
pub fn unexpected_request(peer: PeerId) -> OutEvent {
|
||||||
|
OutEvent::Failure {
|
||||||
|
peer,
|
||||||
|
error: anyhow!("Unexpected request received"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unexpected_response(peer: PeerId) -> OutEvent {
|
||||||
|
OutEvent::Failure {
|
||||||
|
peer,
|
||||||
|
error: anyhow!("Unexpected response received"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `NetworkBehaviour` that represents an XMR/BTC swap node as Alice.
|
||||||
|
#[derive(NetworkBehaviour)]
|
||||||
|
#[behaviour(out_event = "OutEvent", event_process = false)]
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct Behaviour<LR>
|
||||||
|
where
|
||||||
|
LR: LatestRate + Send + 'static,
|
||||||
|
{
|
||||||
|
pub quote: quote::Behaviour,
|
||||||
|
pub swap_setup: alice::Behaviour<LR>,
|
||||||
|
pub transfer_proof: transfer_proof::Behaviour,
|
||||||
|
pub encrypted_signature: encrypted_signature::Behaviour,
|
||||||
|
|
||||||
|
/// Ping behaviour that ensures that the underlying network connection
|
||||||
|
/// is still alive. If the ping fails a connection close event
|
||||||
|
/// will be emitted that is picked up as swarm event.
|
||||||
|
ping: Ping,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<LR> Behaviour<LR>
|
||||||
|
where
|
||||||
|
LR: LatestRate + Send + 'static,
|
||||||
|
{
|
||||||
|
pub fn new(
|
||||||
|
min_buy: bitcoin::Amount,
|
||||||
|
max_buy: bitcoin::Amount,
|
||||||
|
latest_rate: LR,
|
||||||
|
resume_only: bool,
|
||||||
|
env_config: env::Config,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
quote: quote::asb(),
|
||||||
|
swap_setup: alice::Behaviour::new(
|
||||||
|
min_buy,
|
||||||
|
max_buy,
|
||||||
|
env_config,
|
||||||
|
latest_rate,
|
||||||
|
resume_only,
|
||||||
|
),
|
||||||
|
transfer_proof: transfer_proof::alice(),
|
||||||
|
encrypted_signature: encrypted_signature::alice(),
|
||||||
|
ping: Ping::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PingEvent> for OutEvent {
|
||||||
|
fn from(_: PingEvent) -> Self {
|
||||||
|
OutEvent::Other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +0,0 @@
|
|||||||
use crate::network::transport::authenticate_and_multiplex;
|
|
||||||
use anyhow::Result;
|
|
||||||
use libp2p::core::muxing::StreamMuxerBox;
|
|
||||||
use libp2p::core::transport::Boxed;
|
|
||||||
use libp2p::dns::TokioDnsConfig;
|
|
||||||
use libp2p::tcp::TokioTcpConfig;
|
|
||||||
use libp2p::websocket::WsConfig;
|
|
||||||
use libp2p::{identity, PeerId, Transport};
|
|
||||||
|
|
||||||
/// Creates the libp2p transport for the ASB.
|
|
||||||
pub fn new(identity: &identity::Keypair) -> Result<Boxed<(PeerId, StreamMuxerBox)>> {
|
|
||||||
let tcp = TokioTcpConfig::new().nodelay(true);
|
|
||||||
let tcp_with_dns = TokioDnsConfig::system(tcp)?;
|
|
||||||
let websocket_with_dns = WsConfig::new(tcp_with_dns.clone());
|
|
||||||
|
|
||||||
let transport = tcp_with_dns.or_transport(websocket_with_dns).boxed();
|
|
||||||
|
|
||||||
authenticate_and_multiplex(transport, identity)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user