Add ping protocol to ensure connection is alive

Adds the ping behaviour to both ASB and CLI behaviour that periodically pings a connected party to ensure that the underlying network connection is still alive.
This fixes problems with long-running connections that become dead without a connection closure being reported back to the swarm.
This commit is contained in:
Daniel Karzel 2021-05-26 11:40:55 +10:00
parent 6743801c71
commit c9064d5a37
No known key found for this signature in database
GPG key ID: 30C3FC2E438ADB6E
5 changed files with 49 additions and 1 deletions

View file

@ -29,7 +29,7 @@ ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", features = [ "libs
ed25519-dalek = "1"
futures = { version = "0.3", default-features = false }
itertools = "0.10"
libp2p = { version = "0.38", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket" ] }
libp2p = { version = "0.38", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping" ] }
libp2p-async-await = { git = "https://github.com/comit-network/rust-libp2p-async-await" }
miniscript = { version = "5", features = [ "serde" ] }
monero = { version = "0.12", features = [ "serde_support" ] }

View file

@ -4,6 +4,7 @@ use crate::protocol::alice::event_loop::LatestRate;
use crate::protocol::alice::{execution_setup, spot_price, State3};
use crate::{env, monero};
use anyhow::{anyhow, Error};
use libp2p::ping::{Ping, PingEvent};
use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::{NetworkBehaviour, PeerId};
use uuid::Uuid;
@ -75,6 +76,11 @@ where
pub execution_setup: execution_setup::Behaviour,
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>
@ -104,6 +110,13 @@ where
execution_setup: Default::default(),
transfer_proof: transfer_proof::alice(),
encrypted_signature: encrypted_signature::alice(),
ping: Ping::default(),
}
}
}
impl From<PingEvent> for OutEvent {
fn from(_: PingEvent) -> Self {
OutEvent::Other
}
}

View file

@ -4,6 +4,7 @@ use crate::protocol::bob;
use crate::protocol::bob::{execution_setup, State2};
use anyhow::{anyhow, Error, Result};
use libp2p::core::Multiaddr;
use libp2p::ping::{Ping, PingEvent};
use libp2p::request_response::{RequestId, ResponseChannel};
use libp2p::{NetworkBehaviour, PeerId};
use std::time::Duration;
@ -66,6 +67,11 @@ pub struct Behaviour {
pub transfer_proof: transfer_proof::Behaviour,
pub encrypted_signature: encrypted_signature::Behaviour,
pub redial: redial::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 Behaviour {
@ -77,6 +83,7 @@ impl Behaviour {
transfer_proof: transfer_proof::bob(),
encrypted_signature: encrypted_signature::bob(),
redial: redial::Behaviour::new(alice, Duration::from_secs(2)),
ping: Ping::default(),
}
}
@ -88,3 +95,9 @@ impl Behaviour {
self.encrypted_signature.add_address(&peer_id, address);
}
}
impl From<PingEvent> for OutEvent {
fn from(_: PingEvent) -> Self {
OutEvent::Other
}
}