Skip to content

Commit

Permalink
Send HandshakeFailure message if genesis mismatch or wrong protocol (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lexfrl authored Aug 30, 2019
1 parent f47a38b commit 86ec8db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 32 additions & 1 deletion chain/network/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::rate_counter::RateCounter;
use crate::types::{
Ban, Consolidate, Handshake, NetworkClientMessages, PeerChainInfo, PeerInfo, PeerMessage,
PeerStatsResult, PeerStatus, PeerType, PeersRequest, PeersResponse, QueryPeerStats,
ReasonForBan, SendMessage, Unregister,
ReasonForBan, SendMessage, Unregister, HandshakeFailureReason, PROTOCOL_VERSION
};
use crate::{NetworkClientResponses, PeerManagerActor};

Expand Down Expand Up @@ -265,6 +265,7 @@ impl Peer {
}
}
PeerMessage::Handshake(_)
| PeerMessage::HandshakeFailure(_, _)
| PeerMessage::PeersRequest
| PeerMessage::PeersResponse(_) => {
error!(target: "network", "Peer receive_client_message received unexpected type");
Expand Down Expand Up @@ -358,8 +359,38 @@ impl StreamHandler<Vec<u8>, io::Error> for Peer {
}
};
match (self.peer_type, self.peer_status, peer_msg) {
(_, PeerStatus::Connecting, PeerMessage::HandshakeFailure(peer_info, reason)) => {
match reason {
HandshakeFailureReason::GenesisMismatch(genesis) => {
error!(target: "network", "Attempting to connect to a node ({}) with a different genesis block. Our genesis: {}, their genesis: {}", peer_info, self.genesis, genesis);
},
HandshakeFailureReason::ProtocolVersionMismatch(version) => {
error!(target: "network", "Unable to connect to a node ({}) due to a network protocol version mismatch. Our version: {}, their: {}", peer_info, PROTOCOL_VERSION, version);
}
}
ctx.stop();
}
(_, PeerStatus::Connecting, PeerMessage::Handshake(handshake)) => {
debug!(target: "network", "{:?}: Received handshake {:?}", self.node_info.id, handshake);

if handshake.chain_info.genesis != self.genesis {
info!(target: "network", "Received connection from node with different genesis.");
ctx.address().do_send(SendMessage {
message: PeerMessage::HandshakeFailure(
self.node_info.clone(),
HandshakeFailureReason::GenesisMismatch(self.genesis))
});
return;
// Connection will be closed by a handshake timeout
}
if handshake.version != PROTOCOL_VERSION {
info!(target: "network", "Received connection from node with different network protocol version.");
ctx.address().do_send(SendMessage {
message: PeerMessage::HandshakeFailure(
self.node_info.clone(),
HandshakeFailureReason::ProtocolVersionMismatch(PROTOCOL_VERSION))
});
}
if handshake.chain_info.genesis != self.genesis {
info!(target: "network", "Received connection from node with different genesis.");
ctx.stop();
Expand Down
8 changes: 8 additions & 0 deletions chain/network/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,16 @@ impl AnnounceAccount {
}
}

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub enum HandshakeFailureReason {
ProtocolVersionMismatch(u32),
GenesisMismatch(CryptoHash),
}

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub enum PeerMessage {
Handshake(Handshake),
HandshakeFailure(PeerInfo, HandshakeFailureReason),

PeersRequest,
PeersResponse(Vec<PeerInfo>),
Expand All @@ -283,6 +290,7 @@ impl fmt::Display for PeerMessage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PeerMessage::Handshake(_) => f.write_str("Handshake"),
PeerMessage::HandshakeFailure(_, _) => f.write_str("HandshakeFailure"),
PeerMessage::PeersRequest => f.write_str("PeersRequest"),
PeerMessage::PeersResponse(_) => f.write_str("PeersResponse"),
PeerMessage::BlockHeadersRequest(_) => f.write_str("BlockHeaderRequest"),
Expand Down

0 comments on commit 86ec8db

Please sign in to comment.