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

feat(upgradability): Reintroducing handshake #3290

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 3 additions & 2 deletions chain/client/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ mod test {
use near_crypto::{KeyType, PublicKey};
use near_network::routing::EdgeInfo;
use near_network::test_utils::MockNetworkAdapter;
use near_network::types::PeerChainInfo;
use near_network::types::PeerChainInfoV2;
use near_network::PeerInfo;
use near_primitives::block::{Approval, Block, GenesisId};
use near_primitives::network::PeerId;
Expand Down Expand Up @@ -1239,13 +1239,14 @@ mod test {
let mut sync_status = SyncStatus::NoSync;
let peer1 = FullPeerInfo {
peer_info: PeerInfo::random(),
chain_info: PeerChainInfo {
chain_info: PeerChainInfoV2 {
genesis_id: GenesisId {
chain_id: "unittest".to_string(),
hash: *chain.genesis().hash(),
},
height: chain2.head().unwrap().height,
tracked_shards: vec![],
archival: false,
},
edge_info: EdgeInfo::default(),
};
Expand Down
5 changes: 3 additions & 2 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use near_network::recorder::MetricRecorder;
use near_network::routing::EdgeInfo;
use near_network::types::{
AccountOrPeerIdOrHash, NetworkInfo, NetworkViewClientMessages, NetworkViewClientResponses,
PeerChainInfo,
PeerChainInfoV2,
};
use near_network::{
FullPeerInfo, NetworkAdapter, NetworkClientMessages, NetworkClientResponses, NetworkRecipient,
Expand Down Expand Up @@ -434,13 +434,14 @@ pub fn setup_mock_all_validators(
.enumerate()
.map(|(i, peer_info)| FullPeerInfo {
peer_info: peer_info.clone(),
chain_info: PeerChainInfo {
chain_info: PeerChainInfoV2 {
genesis_id: GenesisId {
chain_id: "unittest".to_string(),
hash: Default::default(),
},
height: last_height2[i],
tracked_shards: vec![],
archival: false,
},
edge_info: EdgeInfo::default(),
})
Expand Down
8 changes: 5 additions & 3 deletions chain/client/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use near_logger_utils::init_test_logger;
use near_network::recorder::MetricRecorder;
use near_network::routing::EdgeInfo;
use near_network::test_utils::{wait_or_panic, MockNetworkAdapter};
use near_network::types::{NetworkInfo, PeerChainInfo, ReasonForBan};
use near_network::types::{NetworkInfo, PeerChainInfoV2, ReasonForBan};
use near_network::{
FullPeerInfo, NetworkClientMessages, NetworkClientResponses, NetworkRequests, NetworkResponses,
PeerInfo,
Expand Down Expand Up @@ -761,21 +761,23 @@ fn client_sync_headers() {
client.do_send(NetworkClientMessages::NetworkInfo(NetworkInfo {
active_peers: vec![FullPeerInfo {
peer_info: peer_info2.clone(),
chain_info: PeerChainInfo {
chain_info: PeerChainInfoV2 {
genesis_id: Default::default(),
height: 5,
tracked_shards: vec![],
archival: false,
},
edge_info: EdgeInfo::default(),
}],
num_active_peers: 1,
peer_max_count: 1,
highest_height_peers: vec![FullPeerInfo {
peer_info: peer_info2.clone(),
chain_info: PeerChainInfo {
chain_info: PeerChainInfoV2 {
genesis_id: Default::default(),
height: 5,
tracked_shards: vec![],
archival: false,
},
edge_info: EdgeInfo::default(),
}],
Expand Down
8 changes: 5 additions & 3 deletions chain/network/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ mod test {

use crate::routing::EdgeInfo;
use crate::types::{
Handshake, HandshakeFailureReason, HandshakeV2, PeerChainInfo, PeerIdOrHash, PeerInfo,
RoutedMessage, RoutedMessageBody, SyncData,
Handshake, HandshakeFailureReason, HandshakeV2, PeerChainInfo, PeerChainInfoV2,
PeerIdOrHash, PeerInfo, RoutedMessage, RoutedMessageBody, SyncData,
};

use super::*;
Expand All @@ -106,13 +106,15 @@ mod test {
let peer_info = PeerInfo::random();
let fake_handshake = Handshake {
version: PROTOCOL_VERSION,
oldest_supported_version: OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION,
peer_id: peer_info.id.clone(),
target_peer_id: peer_info.id,
listen_port: None,
chain_info: PeerChainInfo {
chain_info: PeerChainInfoV2 {
genesis_id: Default::default(),
height: 0,
tracked_shards: vec![],
archival: false,
},
edge_info: EdgeInfo::default(),
};
Expand Down
66 changes: 42 additions & 24 deletions chain/network/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use near_primitives::network::PeerId;
use near_primitives::unwrap_option_or_return;
use near_primitives::utils::DisplayOption;
use near_primitives::version::{
ProtocolVersion, NETWORK_PROTOCOL_VERSION, OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION,
ProtocolVersion, OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION, PROTOCOL_VERSION,
};

use crate::codec::{bytes_to_peer_message, peer_message_to_bytes, Codec};
Expand All @@ -26,12 +26,12 @@ use crate::rate_counter::RateCounter;
use crate::recorder::{PeerMessageMetadata, Status};
use crate::routing::{Edge, EdgeInfo};
use crate::types::{
Ban, Consolidate, ConsolidateResponse, HandshakeFailureReason, HandshakeV2,
Ban, Consolidate, ConsolidateResponse, Handshake, HandshakeFailureReason, HandshakeV2,
NetworkClientMessages, NetworkClientResponses, NetworkRequests, NetworkViewClientMessages,
NetworkViewClientResponses, PeerChainInfo, PeerInfo, PeerManagerRequest, PeerMessage,
PeerRequest, PeerResponse, PeerStatsResult, PeerStatus, PeerType, PeersRequest, PeersResponse,
QueryPeerStats, ReasonForBan, RoutedMessageBody, RoutedMessageFrom, SendMessage, Unregister,
UPDATE_INTERVAL_LAST_TIME_RECEIVED_MESSAGE,
NetworkViewClientResponses, PeerChainInfo, PeerChainInfoV2, PeerInfo, PeerManagerRequest,
PeerMessage, PeerRequest, PeerResponse, PeerStatsResult, PeerStatus, PeerType, PeersRequest,
PeersResponse, QueryPeerStats, ReasonForBan, RoutedMessageBody, RoutedMessageFrom, SendMessage,
Unregister, UPDATE_INTERVAL_LAST_TIME_RECEIVED_MESSAGE,
};
use crate::PeerManagerActor;
use crate::{metrics, NetworkResponses};
Expand Down Expand Up @@ -158,7 +158,7 @@ pub struct Peer {
/// This node genesis id.
genesis_id: GenesisId,
/// Latest chain info from the peer.
chain_info: PeerChainInfo,
chain_info: PeerChainInfoV2,
/// Edge information needed to build the real edge. This is relevant for handshake.
edge_info: Option<EdgeInfo>,
/// Last time an update of received message was sent to PeerManager
Expand Down Expand Up @@ -187,7 +187,7 @@ impl Peer {
peer_info: peer_info.into(),
peer_type,
peer_status: PeerStatus::Connecting,
protocol_version: NETWORK_PROTOCOL_VERSION,
protocol_version: PROTOCOL_VERSION,
framed,
handshake_timeout,
peer_manager_addr,
Expand Down Expand Up @@ -274,15 +274,33 @@ impl Peer {
height,
tracked_shards,
}) => {
let handshake = HandshakeV2::new(
act.protocol_version,
act.node_id(),
act.peer_id().unwrap(),
act.node_info.addr_port(),
PeerChainInfo { genesis_id, height, tracked_shards },
act.edge_info.as_ref().unwrap().clone(),
);
act.send_message(PeerMessage::HandshakeV2(handshake));
// TODO(MOO): Fill this here
let archival = false;

let handshake = match act.protocol_version {
36 => PeerMessage::Handshake(Handshake::new(
act.protocol_version,
act.node_id(),
act.peer_id().unwrap(),
act.node_info.addr_port(),
PeerChainInfoV2 { genesis_id, height, tracked_shards, archival },
act.edge_info.as_ref().unwrap().clone(),
)),
34..=35 => PeerMessage::HandshakeV2(HandshakeV2::new(
act.protocol_version,
act.node_id(),
act.peer_id().unwrap(),
act.node_info.addr_port(),
PeerChainInfo { genesis_id, height, tracked_shards },
act.edge_info.as_ref().unwrap().clone(),
)),
_ => {
error!(target: "network", "Trying to talk with peer with no supported version: {}", act.protocol_version);
return actix::fut::ready(());
}
};

act.send_message(handshake);
actix::fut::ready(())
}
Err(err) => {
Expand Down Expand Up @@ -631,7 +649,7 @@ impl StreamHandler<Result<Vec<u8>, ReasonForBan>> for Peer {
self.send_message(PeerMessage::HandshakeFailure(
self.node_info.clone(),
HandshakeFailureReason::ProtocolVersionMismatch {
version: NETWORK_PROTOCOL_VERSION,
version: PROTOCOL_VERSION,
oldest_supported_version: OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION,
},
));
Expand Down Expand Up @@ -667,8 +685,8 @@ impl StreamHandler<Result<Vec<u8>, ReasonForBan>> for Peer {
msg.len() as i64,
);

if let PeerMessage::Handshake(handshake) = peer_msg {
peer_msg = PeerMessage::HandshakeV2(handshake.into());
if let PeerMessage::HandshakeV2(handshake) = peer_msg {
peer_msg = PeerMessage::Handshake(handshake.into());
}

match (self.peer_type, self.peer_status, peer_msg) {
Expand All @@ -681,7 +699,7 @@ impl StreamHandler<Result<Vec<u8>, ReasonForBan>> for Peer {
version,
oldest_supported_version,
} => {
let target_version = std::cmp::min(version, NETWORK_PROTOCOL_VERSION);
let target_version = std::cmp::min(version, PROTOCOL_VERSION);

if target_version
>= std::cmp::max(
Expand All @@ -694,7 +712,7 @@ impl StreamHandler<Result<Vec<u8>, ReasonForBan>> for Peer {
self.send_handshake(ctx);
return;
} else {
warn!(target: "network", "Unable to connect to a node ({}) due to a network protocol version mismatch. Our version: {:?}, their: {:?}", peer_info, (NETWORK_PROTOCOL_VERSION, OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION), (version, oldest_supported_version));
warn!(target: "network", "Unable to connect to a node ({}) due to a network protocol version mismatch. Our version: {:?}, their: {:?}", peer_info, (PROTOCOL_VERSION, OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION), (version, oldest_supported_version));
}
}
HandshakeFailureReason::InvalidTarget => {
Expand All @@ -704,12 +722,12 @@ impl StreamHandler<Result<Vec<u8>, ReasonForBan>> for Peer {
}
ctx.stop();
}
(_, PeerStatus::Connecting, PeerMessage::HandshakeV2(handshake)) => {
(_, PeerStatus::Connecting, PeerMessage::Handshake(handshake)) => {
debug!(target: "network", "{:?}: Received handshake {:?}", self.node_info.id, handshake);

debug_assert!(
OLDEST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION <= handshake.version
&& handshake.version <= NETWORK_PROTOCOL_VERSION
&& handshake.version <= PROTOCOL_VERSION
);

if handshake.chain_info.genesis_id != self.genesis_id {
Expand Down
Loading