Skip to content

Commit

Permalink
feat(network): implement stand-alone peer handshake query (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
lancevincentsalera authored Feb 5, 2025
1 parent 510dcb7 commit 5a73fd6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
43 changes: 42 additions & 1 deletion pallas-network/src/facades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use tokio::net::{TcpListener, ToSocketAddrs};
#[cfg(unix)]
use tokio::net::{unix::SocketAddr as UnixSocketAddr, UnixListener};

use crate::miniprotocols::handshake::{n2c, n2n, Confirmation, VersionNumber};
use crate::miniprotocols::handshake::n2n::VersionData;
use crate::miniprotocols::handshake::{n2c, n2n, Confirmation, VersionNumber, VersionTable};

use crate::miniprotocols::{
blockfetch, chainsync, handshake, keepalive, localstate, localtxsubmission, peersharing,
Expand Down Expand Up @@ -159,6 +160,46 @@ impl PeerClient {
Ok(client)
}

pub async fn handshake_query(
addr: impl ToSocketAddrs,
magic: u64,
) -> Result<VersionTable<VersionData>, Error> {
let bearer = Bearer::connect_tcp(addr)
.await
.map_err(Error::ConnectFailure)?;

let mut plexer = multiplexer::Plexer::new(bearer);

let channel = plexer.subscribe_client(PROTOCOL_N2N_HANDSHAKE);
let mut handshake = handshake::Client::new(channel);

let _plexer = plexer.spawn();

let versions = handshake::n2n::VersionTable::v7_and_above_with_query(magic, true);

let handshake = handshake
.handshake(versions)
.await
.map_err(Error::HandshakeProtocol)?;

let version_table = match handshake {
handshake::Confirmation::QueryReply(version_table) => {
debug!("handshake query reply received");
version_table
}
handshake::Confirmation::Accepted(_, _) => {
error!("handshake accepted when we expected query reply");
return Err(Error::HandshakeProtocol(handshake::Error::InvalidInbound));
}
handshake::Confirmation::Rejected(reason) => {
error!(?reason, "handshake refused");
return Err(Error::IncompatibleVersion);
}
};

Ok(version_table)
}

pub fn chainsync(&mut self) -> &mut chainsync::N2NClient {
&mut self.chainsync
}
Expand Down
28 changes: 18 additions & 10 deletions pallas-network/src/miniprotocols/handshake/n2n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ impl VersionTable {
}

pub fn v7_and_above(network_magic: u64) -> VersionTable {
Self::v7_and_above_with_query(network_magic, false)
}

pub fn v7_and_above_with_query(network_magic: u64, query: bool) -> VersionTable {
let values = vec![
(
PROTOCOL_V7,
Expand All @@ -78,7 +82,7 @@ impl VersionTable {
network_magic,
true,
Some(PEER_SHARING_DISABLED),
Some(false),
Some(query),
),
),
(
Expand All @@ -87,7 +91,7 @@ impl VersionTable {
network_magic,
true,
Some(PEER_SHARING_DISABLED),
Some(false),
Some(query),
),
),
(
Expand All @@ -96,7 +100,7 @@ impl VersionTable {
network_magic,
true,
Some(PEER_SHARING_DISABLED),
Some(false),
Some(query),
),
),
]
Expand All @@ -107,14 +111,18 @@ impl VersionTable {
}

pub fn v11_and_above(network_magic: u64) -> VersionTable {
Self::v11_and_above_with_query(network_magic, false)
}

pub fn v11_and_above_with_query(network_magic: u64, query: bool) -> VersionTable {
let values = vec![
(
PROTOCOL_V11,
VersionData::new(
network_magic,
true,
Some(PEER_SHARING_DISABLED),
Some(false),
Some(query),
),
),
(
Expand All @@ -123,7 +131,7 @@ impl VersionTable {
network_magic,
true,
Some(PEER_SHARING_DISABLED),
Some(false),
Some(query),
),
),
(
Expand All @@ -132,7 +140,7 @@ impl VersionTable {
network_magic,
true,
Some(PEER_SHARING_DISABLED),
Some(false),
Some(query),
),
),
]
Expand All @@ -145,10 +153,10 @@ impl VersionTable {

#[derive(Debug, Clone, PartialEq)]
pub struct VersionData {
network_magic: u64,
initiator_only_diffusion_mode: bool,
peer_sharing: Option<u8>,
query: Option<bool>,
pub network_magic: u64,
pub initiator_only_diffusion_mode: bool,
pub peer_sharing: Option<u8>,
pub query: Option<bool>,
}

impl VersionData {
Expand Down

0 comments on commit 5a73fd6

Please sign in to comment.