Skip to content

Commit

Permalink
Add borsh support for PeerMessage::DistanceVector (near#9348)
Browse files Browse the repository at this point in the history
In near#9187 we introduced the first new PeerMessage variant in a long time, called DistanceVector.

I got a little over-zealous about our plans to deprecate borsh and [skipped implementing borsh support for the new message variant](https://github.com/saketh-are/nearcore/blob/2093819d414bd38c73574c681715e3a544daa945/chain/network/src/network_protocol/borsh_conv.rs#L180-L182).

However, it turns out we have some test infrastructure still reliant on borsh-encoded connections:
https://github.com/near/nearcore/blob/6cdee7cc123bdeb00f0d9029b10f8c1448eab54f/pytest/lib/proxy.py#L89-L90

In particular, the nayduck test `pytest/tests/sanity/sync_chunks_from_archival.py` makes use of the proxy tool and [is failing]( https://nayduck.near.org/#/test/497500) after near#9187. 

This PR implements borsh support for DistanceVector as an immediate fix for the failing test.

In the long run we aim to deprecate borsh entirely, at which time this code (and a bunch of other code much like it) will be removed.
  • Loading branch information
saketh-are authored and nikurt committed Jul 26, 2023
1 parent 99ccfea commit e08de4c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
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

0 comments on commit e08de4c

Please sign in to comment.