Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add borsh support for PeerMessage::DistanceVector #9348

Merged
merged 2 commits into from
Jul 26, 2023
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
15 changes: 15 additions & 0 deletions chain/network/src/network_protocol/borsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ pub(super) struct RoutingTableUpdate {
pub accounts: Vec<AnnounceAccount>,
}

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub struct AdvertisedPeerDistance {
pub destination: PeerId,
pub distance: u32,
}

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub(super) struct DistanceVector {
pub root: PeerId,
pub distances: Vec<AdvertisedPeerDistance>,
pub edges: Vec<Edge>,
}

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub enum HandshakeFailureReason {
ProtocolVersionMismatch { version: u32, oldest_supported_version: u32 },
Expand Down Expand Up @@ -139,6 +152,8 @@ pub(super) enum PeerMessage {
_EpochSyncFinalizationRequest,
_EpochSyncFinalizationResponse,
_RoutingTableSyncV2,

DistanceVector(DistanceVector),
}
#[cfg(target_arch = "x86_64")] // Non-x86_64 doesn't match this requirement yet but it's not bad as it's not production-ready
const _: () = assert!(std::mem::size_of::<PeerMessage>() <= 1144, "PeerMessage > 1144 bytes");
41 changes: 38 additions & 3 deletions chain/network/src/network_protocol/borsh_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ impl From<mem::RoutingTableUpdate> for net::RoutingTableUpdate {

//////////////////////////////////////////

impl From<net::AdvertisedPeerDistance> for mem::AdvertisedPeerDistance {
fn from(x: net::AdvertisedPeerDistance) -> Self {
Self { destination: x.destination, distance: x.distance }
}
}

impl From<mem::AdvertisedPeerDistance> for net::AdvertisedPeerDistance {
fn from(x: mem::AdvertisedPeerDistance) -> Self {
Self { destination: x.destination, distance: x.distance }
}
}

//////////////////////////////////////////

impl From<net::DistanceVector> for mem::DistanceVector {
fn from(x: net::DistanceVector) -> Self {
Self {
root: x.root,
distances: x.distances.into_iter().map(|y| y.into()).collect(),
edges: x.edges,
}
}
}

impl From<mem::DistanceVector> for net::DistanceVector {
fn from(x: mem::DistanceVector) -> Self {
Self {
root: x.root,
distances: x.distances.into_iter().map(|y| y.into()).collect(),
edges: x.edges,
}
}
}

//////////////////////////////////////////

#[derive(thiserror::Error, Debug)]
pub enum ParsePeerMessageError {
#[error("HandshakeV2 is deprecated")]
Expand Down Expand Up @@ -156,6 +192,7 @@ impl TryFrom<&net::PeerMessage> for mem::PeerMessage {
net::PeerMessage::_RoutingTableSyncV2 => {
return Err(Self::Error::DeprecatedRoutingTableSyncV2)
}
net::PeerMessage::DistanceVector(dv) => mem::PeerMessage::DistanceVector(dv.into()),
})
}
}
Expand All @@ -177,9 +214,7 @@ impl From<&mem::PeerMessage> for net::PeerMessage {
net::PeerMessage::SyncRoutingTable(rtu.into())
}
mem::PeerMessage::RequestUpdateNonce(e) => net::PeerMessage::RequestUpdateNonce(e),
mem::PeerMessage::DistanceVector(_) => {
panic!("DistanceVector is not supported in Borsh encoding")
}
mem::PeerMessage::DistanceVector(dv) => net::PeerMessage::DistanceVector(dv.into()),

// This message is not supported, we translate it to an empty RoutingTableUpdate.
mem::PeerMessage::SyncAccountsData(_) => {
Expand Down