From e08de4c8ab843eabdea6650bbbfe6d60467d0678 Mon Sep 17 00:00:00 2001 From: Saketh Are Date: Tue, 25 Jul 2023 23:14:36 -0400 Subject: [PATCH] Add borsh support for PeerMessage::DistanceVector (#9348) In https://github.com/near/nearcore/pull/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 #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. --- chain/network/src/network_protocol/borsh.rs | 15 +++++++ .../src/network_protocol/borsh_conv.rs | 41 +++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/chain/network/src/network_protocol/borsh.rs b/chain/network/src/network_protocol/borsh.rs index 635c01665a7..9e7d3a4c5e6 100644 --- a/chain/network/src/network_protocol/borsh.rs +++ b/chain/network/src/network_protocol/borsh.rs @@ -82,6 +82,19 @@ pub(super) struct RoutingTableUpdate { pub accounts: Vec, } +#[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, + pub edges: Vec, +} + #[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)] pub enum HandshakeFailureReason { ProtocolVersionMismatch { version: u32, oldest_supported_version: u32 }, @@ -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::() <= 1144, "PeerMessage > 1144 bytes"); diff --git a/chain/network/src/network_protocol/borsh_conv.rs b/chain/network/src/network_protocol/borsh_conv.rs index de3c579312b..4eca252bb0e 100644 --- a/chain/network/src/network_protocol/borsh_conv.rs +++ b/chain/network/src/network_protocol/borsh_conv.rs @@ -90,6 +90,42 @@ impl From for net::RoutingTableUpdate { ////////////////////////////////////////// +impl From for mem::AdvertisedPeerDistance { + fn from(x: net::AdvertisedPeerDistance) -> Self { + Self { destination: x.destination, distance: x.distance } + } +} + +impl From for net::AdvertisedPeerDistance { + fn from(x: mem::AdvertisedPeerDistance) -> Self { + Self { destination: x.destination, distance: x.distance } + } +} + +////////////////////////////////////////// + +impl From 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 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")] @@ -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()), }) } } @@ -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(_) => {