Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ serial_test = "3.2.0"
sha2 = "0.10.9"
sha3 = "0.10.8"
signature = "2.2.0"
socket2 = "0.6.1"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised we need a depdency for this, but this looks like the way to do it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's from the rust-lang team so should be fine

syn = "2.0"
tempfile = "3.22.0"
thiserror = "2.0.12"
Expand Down
1 change: 1 addition & 0 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
sha3 = { workspace = true }
socket2 = { workspace = true }
tee-authority = { workspace = true }
tempfile = { workspace = true }
test-utils = { workspace = true }
Expand Down
41 changes: 30 additions & 11 deletions crates/node/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ use tracing::info;
/// the cost of higher packet overhead.
const TCP_NODELAY: bool = true;

const TCP_CONNECTION_KEEPALIVE_DELAY: std::time::Duration = std::time::Duration::from_secs(3);
const TCP_CONNECTION_RETRY_DELAY: std::time::Duration = std::time::Duration::from_secs(1);
const TCP_CONNECTION_RETRIES: u32 = 3;

const PING_KEEPALIVE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(5);

/// Implements MeshNetworkTransportSender for sending messages over a TLS-based
/// mesh network.
pub struct TlsMeshSender {
Expand Down Expand Up @@ -110,15 +116,13 @@ impl TlsConnection {
target_participant_id: ParticipantId,
participant_identities: &ParticipantIdentities,
) -> anyhow::Result<TlsConnection> {
let conn = TcpStream::connect(target_address)
let tcp_stream = TcpStream::connect(target_address)
.await
.context("TCP connect")?;

conn.set_nodelay(TCP_NODELAY)
.context("failed to enable `TCP_NODELAY`")?;
let tcp_stream = configure_tcp_stream(tcp_stream)?;

let mut tls_conn = tokio_rustls::TlsConnector::from(client_config)
.connect("dummy".try_into().unwrap(), conn)
.connect("dummy".try_into().unwrap(), tcp_stream)
.await
.context("TLS connect")?;

Expand Down Expand Up @@ -174,10 +178,10 @@ impl TlsConnection {
);
let sender_clone = sender.clone();
let keepalive_task = tracking::spawn(
&format!("TCP keepalive for {}", target_participant_id),
&format!("ping keepalive task for {}", target_participant_id),
async move {
loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
tokio::time::sleep(PING_KEEPALIVE_INTERVAL).await;
if sender_clone.send(Packet::Ping).is_err() {
// The receiver side will be dropped when the sender task is
// dropped (i.e. connection is closed).
Expand Down Expand Up @@ -370,11 +374,9 @@ pub async fn new_tls_mesh_network(
let tls_acceptor = tls_acceptor.clone();
let connectivities = connectivities_clone.clone();
tasks.spawn_checked::<_, ()>("Handle connection", async move {
tcp_stream
.set_nodelay(TCP_NODELAY)
.context("failed to enable `TCP_NODELAY` for incoming TCP stream")?;

let tcp_stream = configure_tcp_stream(tcp_stream)?;
let mut stream = tls_acceptor.accept(tcp_stream).await?;

let peer_id = verify_peer_identity(stream.get_ref().1, &participant_identities)?;
tracking::set_progress(&format!("Authenticated as {}", peer_id));
p2p_handshake(&mut stream, TlsConnection::HANDSHAKE_TIMEOUT)
Expand Down Expand Up @@ -454,6 +456,23 @@ pub async fn new_tls_mesh_network(
Ok((sender, receiver))
}

fn configure_tcp_stream(tcp_stream: TcpStream) -> anyhow::Result<TcpStream> {
let socket = socket2::Socket::from(tcp_stream.into_std()?);
let keepalive = socket2::TcpKeepalive::new()
.with_time(TCP_CONNECTION_KEEPALIVE_DELAY)
.with_interval(TCP_CONNECTION_RETRY_DELAY)
.with_retries(TCP_CONNECTION_RETRIES);

socket
.set_tcp_keepalive(&keepalive)
.context("failed to set TCP keepalive")?;
socket
.set_tcp_nodelay(TCP_NODELAY)
.context("failed to enable `TCP_NODELAY`")?;

Ok(TcpStream::from_std(socket.into())?)
}

fn verify_peer_identity(
conn: &CommonState,
participant_identities: &ParticipantIdentities,
Expand Down