wip: migrate to libp2p 0.53.2

This commit is contained in:
binarybaron 2024-08-07 12:54:28 +02:00
parent b18ba95e8c
commit a56435aa8d
No known key found for this signature in database
GPG Key ID: 99B75D3E1476A26E
9 changed files with 1788 additions and 632 deletions

2344
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = [ "monero-harness", "monero-rpc", "swap", "monero-wallet" ] members = [ "monero-harness", "monero-rpc", "swap", "monero-wallet", "watchtower" ]
[patch.crates-io] [patch.crates-io]
# patch until new release https://github.com/thomaseizinger/rust-jsonrpc-client/pull/51 # patch until new release https://github.com/thomaseizinger/rust-jsonrpc-client/pull/51

View File

@ -34,9 +34,10 @@ hex = "0.4"
itertools = "0.13" itertools = "0.13"
jsonrpsee = { version = "0.16.2", features = [ "server" ] } jsonrpsee = { version = "0.16.2", features = [ "server" ] }
jsonrpsee-core = "0.16.2" jsonrpsee-core = "0.16.2"
libp2p = { version = "0.42.2", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous", "identify" ] } libp2p = { version = "0.53.2", default-features = false, features = [ "tcp", "yamux", "dns", "noise", "request-response", "websocket", "ping", "rendezvous", "identify" ] }
monero = { version = "0.12", features = [ "serde_support" ] } monero = { version = "0.12", features = [ "serde_support" ] }
monero-rpc = { path = "../monero-rpc" } monero-rpc = { path = "../monero-rpc" }
watchtower = { path = "../watchtower" }
pem = "3.0" pem = "3.0"
proptest = "1" proptest = "1"
qrcode = "0.14" qrcode = "0.14"

View File

@ -12,7 +12,7 @@ use anyhow::{Context, Result};
use futures::future; use futures::future;
use futures::future::{BoxFuture, FutureExt}; use futures::future::{BoxFuture, FutureExt};
use futures::stream::{FuturesUnordered, StreamExt}; use futures::stream::{FuturesUnordered, StreamExt};
use libp2p::request_response::{RequestId, ResponseChannel}; use libp2p::request_response::{InboundRequestId, OutboundRequestId, ResponseChannel};
use libp2p::swarm::SwarmEvent; use libp2p::swarm::SwarmEvent;
use libp2p::{PeerId, Swarm}; use libp2p::{PeerId, Swarm};
use rust_decimal::Decimal; use rust_decimal::Decimal;
@ -62,7 +62,7 @@ where
/// Tracks [`transfer_proof::Request`]s which are currently inflight and /// Tracks [`transfer_proof::Request`]s which are currently inflight and
/// awaiting an acknowledgement. /// awaiting an acknowledgement.
inflight_transfer_proofs: HashMap<RequestId, bmrng::Responder<()>>, inflight_transfer_proofs: HashMap<OutboundRequestId, bmrng::Responder<()>>,
} }
impl<LR> EventLoop<LR> impl<LR> EventLoop<LR>
@ -334,10 +334,10 @@ where
SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => { SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => {
tracing::warn!(%address, "Failed to set up connection with peer: {:#}", error); tracing::warn!(%address, "Failed to set up connection with peer: {:#}", error);
} }
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: Some(error) } => { SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: Some(error), connection_id } => {
tracing::debug!(%peer, address = %endpoint.get_remote_address(), "Lost connection to peer: {:#}", error); tracing::debug!(%peer, address = %endpoint.get_remote_address(), "Lost connection to peer: {:#}", error);
} }
SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: None } => { SwarmEvent::ConnectionClosed { peer_id: peer, num_established: 0, endpoint, cause: None, connection_id } => {
tracing::info!(%peer, address = %endpoint.get_remote_address(), "Successfully closed connection"); tracing::info!(%peer, address = %endpoint.get_remote_address(), "Successfully closed connection");
} }
SwarmEvent::NewListenAddr{address, ..} => { SwarmEvent::NewListenAddr{address, ..} => {

View File

@ -11,32 +11,27 @@ use crate::network::{
use crate::protocol::alice::State3; use crate::protocol::alice::State3;
use anyhow::{anyhow, Error, Result}; use anyhow::{anyhow, Error, Result};
use futures::FutureExt; use futures::FutureExt;
use libp2p::core::connection::ConnectionId;
use libp2p::core::muxing::StreamMuxerBox; use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::Boxed; use libp2p::core::transport::Boxed;
use libp2p::dns::TokioDnsConfig; use libp2p::request_response::{ResponseChannel};
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::swarm::dial_opts::PeerCondition; use libp2p::swarm::dial_opts::PeerCondition;
use libp2p::swarm::{ use libp2p::swarm::{
IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters, NetworkBehaviour
ProtocolsHandler,
}; };
use libp2p::tcp::TokioTcpConfig; use libp2p::{Multiaddr, PeerId, Transport};
use libp2p::websocket::WsConfig;
use libp2p::{identity, Multiaddr, NetworkBehaviour, PeerId, Transport};
use std::task::Poll; use std::task::Poll;
use std::time::Duration; use std::time::Duration;
use uuid::Uuid; use uuid::Uuid;
pub mod transport { pub mod transport {
use libp2p::{dns, identity, tcp, websocket::WsConfig};
use super::*; use super::*;
/// Creates the libp2p transport for the ASB. /// Creates the libp2p transport for the ASB.
pub fn new(identity: &identity::Keypair) -> Result<Boxed<(PeerId, StreamMuxerBox)>> { pub fn new(identity: &identity::Keypair) -> Result<Boxed<(PeerId, StreamMuxerBox)>> {
let tcp = TokioTcpConfig::new().nodelay(true); let tcp = tcp::Config::new().nodelay(true);
let tcp_with_dns = TokioDnsConfig::system(tcp)?; let tcp_with_dns = dns::ResolverConfig::default::system(tcp)?;
let websocket_with_dns = WsConfig::new(tcp_with_dns.clone()); let websocket_with_dns = WsConfig::new(tcp_with_dns.clone());
let transport = tcp_with_dns.or_transport(websocket_with_dns).boxed(); let transport = tcp_with_dns.or_transport(websocket_with_dns).boxed();

View File

@ -10,17 +10,16 @@ use crate::protocol::bob::State2;
use crate::{bitcoin, env}; use crate::{bitcoin, env};
use anyhow::{anyhow, Error, Result}; use anyhow::{anyhow, Error, Result};
use libp2p::core::Multiaddr; use libp2p::core::Multiaddr;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent}; use libp2p::request_response::{ResponseChannel, InboundRequestId};
use libp2p::ping::{Ping, PingConfig, PingEvent}; use libp2p::swarm::{NetworkBehaviour};
use libp2p::request_response::{RequestId, ResponseChannel}; use libp2p::{identify, identity, ping, PeerId};
use libp2p::{identity, NetworkBehaviour, PeerId};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
#[derive(Debug)] #[derive(Debug)]
pub enum OutEvent { pub enum OutEvent {
QuoteReceived { QuoteReceived {
id: RequestId, id: InboundRequestId,
response: BidQuote, response: BidQuote,
}, },
SwapSetupCompleted(Box<Result<State2>>), SwapSetupCompleted(Box<Result<State2>>),
@ -30,15 +29,15 @@ pub enum OutEvent {
peer: PeerId, peer: PeerId,
}, },
EncryptedSignatureAcknowledged { EncryptedSignatureAcknowledged {
id: RequestId, id: InboundRequestId,
}, },
CooperativeXmrRedeemFulfilled { CooperativeXmrRedeemFulfilled {
id: RequestId, id: InboundRequestId,
s_a: Scalar, s_a: Scalar,
swap_id: uuid::Uuid, swap_id: uuid::Uuid,
}, },
CooperativeXmrRedeemRejected { CooperativeXmrRedeemRejected {
id: RequestId, id: InboundRequestId,
reason: CooperativeXmrRedeemRejectReason, reason: CooperativeXmrRedeemRejectReason,
swap_id: uuid::Uuid, swap_id: uuid::Uuid,
}, },
@ -81,12 +80,12 @@ pub struct Behaviour {
pub cooperative_xmr_redeem: cooperative_xmr_redeem_after_punish::Behaviour, pub cooperative_xmr_redeem: cooperative_xmr_redeem_after_punish::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour, pub encrypted_signature: encrypted_signature::Behaviour,
pub redial: redial::Behaviour, pub redial: redial::Behaviour,
pub identify: Identify, pub identify: identify::Behaviour,
/// Ping behaviour that ensures that the underlying network connection is /// Ping behaviour that ensures that the underlying network connection is
/// still alive. If the ping fails a connection close event will be /// still alive. If the ping fails a connection close event will be
/// emitted that is picked up as swarm event. /// emitted that is picked up as swarm event.
ping: Ping, ping: ping::Behaviour,
} }
impl Behaviour { impl Behaviour {
@ -98,7 +97,7 @@ impl Behaviour {
) -> Self { ) -> Self {
let agentVersion = format!("cli/{} ({})", env!("CARGO_PKG_VERSION"), identify_params.1); let agentVersion = format!("cli/{} ({})", env!("CARGO_PKG_VERSION"), identify_params.1);
let protocolVersion = "/comit/xmr/btc/1.0.0".to_string(); let protocolVersion = "/comit/xmr/btc/1.0.0".to_string();
let identifyConfig = IdentifyConfig::new(protocolVersion, identify_params.0.public()) let identifyConfig = identify::Config::new(protocolVersion, identify_params.0.public())
.with_agent_version(agentVersion); .with_agent_version(agentVersion);
Self { Self {
@ -121,14 +120,14 @@ impl Behaviour {
} }
} }
impl From<PingEvent> for OutEvent { impl From<ping::Event> for OutEvent {
fn from(_: PingEvent) -> Self { fn from(_: ping::Event) -> Self {
OutEvent::Other OutEvent::Other
} }
} }
impl From<IdentifyEvent> for OutEvent { impl From<identify::Event> for OutEvent {
fn from(_: IdentifyEvent) -> Self { fn from(_: identify::Event) -> Self {
OutEvent::Other OutEvent::Other
} }
} }

View File

@ -1,5 +1,6 @@
use crate::bitcoin::EncryptedSignature; use crate::bitcoin::EncryptedSignature;
use crate::cli::behaviour::{Behaviour, OutEvent}; use crate::cli::behaviour::{Behaviour, OutEvent};
use libp2p::swarm::NetworkBehaviour;
use crate::monero; use crate::monero;
use crate::network::cooperative_xmr_redeem_after_punish::{Request, Response}; use crate::network::cooperative_xmr_redeem_after_punish::{Request, Response};
use crate::network::encrypted_signature; use crate::network::encrypted_signature;
@ -202,10 +203,10 @@ impl EventLoop {
SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } if peer_id == self.alice_peer_id => { SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } if peer_id == self.alice_peer_id => {
tracing::info!(peer_id = %endpoint.get_remote_address(), "Connected to Alice"); tracing::info!(peer_id = %endpoint.get_remote_address(), "Connected to Alice");
} }
SwarmEvent::Dialing(peer_id) if peer_id == self.alice_peer_id => { SwarmEvent::Dialing { peer_id: Some(alice_peer_id), connection_id } if alice_peer_id == self.alice_peer_id => {
tracing::debug!(%peer_id, "Dialling Alice"); tracing::debug!(%alice_peer_id, "Dialling Alice");
} }
SwarmEvent::ConnectionClosed { peer_id, endpoint, num_established, cause: Some(error) } if peer_id == self.alice_peer_id && num_established == 0 => { SwarmEvent::ConnectionClosed { peer_id, endpoint, num_established, cause: Some(error), connection_id } if peer_id == self.alice_peer_id && num_established == 0 => {
tracing::warn!(peer_id = %endpoint.get_remote_address(), cause = %error, "Lost connection to Alice"); tracing::warn!(peer_id = %endpoint.get_remote_address(), cause = %error, "Lost connection to Alice");
} }
SwarmEvent::ConnectionClosed { peer_id, num_established, cause: None, .. } if peer_id == self.alice_peer_id && num_established == 0 => { SwarmEvent::ConnectionClosed { peer_id, num_established, cause: None, .. } if peer_id == self.alice_peer_id && num_established == 0 => {
@ -213,7 +214,7 @@ impl EventLoop {
tracing::info!("Successfully closed connection to Alice"); tracing::info!("Successfully closed connection to Alice");
return; return;
} }
SwarmEvent::OutgoingConnectionError { peer_id: Some(alice_peer_id), error } if alice_peer_id == self.alice_peer_id => { SwarmEvent::OutgoingConnectionError { peer_id: Some(alice_peer_id), error, connection_id } if alice_peer_id == self.alice_peer_id => {
tracing::warn!(%error, "Failed to dial Alice"); tracing::warn!(%error, "Failed to dial Alice");
if let Some(duration) = self.swarm.behaviour_mut().redial.until_next_redial() { if let Some(duration) = self.swarm.behaviour_mut().redial.until_next_redial() {

View File

@ -5,7 +5,6 @@ use libp2p::core::multiaddr::{Multiaddr, Protocol};
use libp2p::core::transport::TransportError; use libp2p::core::transport::TransportError;
use libp2p::core::Transport; use libp2p::core::Transport;
use libp2p::tcp::tokio::{Tcp, TcpStream}; use libp2p::tcp::tokio::{Tcp, TcpStream};
use libp2p::tcp::TcpListenStream;
use std::borrow::Cow; use std::borrow::Cow;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use std::{fmt, io}; use std::{fmt, io};
@ -34,7 +33,7 @@ impl Transport for TorDialOnlyTransport {
Err(TransportError::MultiaddrNotSupported(addr)) Err(TransportError::MultiaddrNotSupported(addr))
} }
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> { fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let address = TorCompatibleAddress::from_multiaddr(Cow::Borrowed(&addr))?; let address = TorCompatibleAddress::from_multiaddr(Cow::Borrowed(&addr))?;
if address.is_certainly_not_reachable_via_tor_daemon() { if address.is_certainly_not_reachable_via_tor_daemon() {
@ -60,7 +59,7 @@ impl Transport for TorDialOnlyTransport {
fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> { fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> {
None None
} }
fn dial_as_listener(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> { fn dial_as_listener(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let address = TorCompatibleAddress::from_multiaddr(Cow::Borrowed(&addr))?; let address = TorCompatibleAddress::from_multiaddr(Cow::Borrowed(&addr))?;
if address.is_certainly_not_reachable_via_tor_daemon() { if address.is_certainly_not_reachable_via_tor_daemon() {

1
watchtower Submodule

@ -0,0 +1 @@
Subproject commit 825166e948ceccc9e6378a5d34fb29b062301327