Send a status message on block announce handshake (#5726)

* Send a status message on block announce handshake

* Make sure to send the handshake to all handlers
This commit is contained in:
Pierre Krieger
2020-04-22 10:20:52 +02:00
committed by GitHub
parent 50a7e12b3f
commit 3ad9f65f11
3 changed files with 100 additions and 2 deletions
@@ -201,7 +201,21 @@ enum PeerState {
}
impl PeerState {
/// True if there exists an established connection to tbe peer
/// True if there exists any established connection to the peer.
fn is_connected(&self) -> bool {
match self {
PeerState::Disabled { .. } |
PeerState::DisabledPendingEnable { .. } |
PeerState::Enabled { .. } |
PeerState::PendingRequest { .. } |
PeerState::Requested |
PeerState::Incoming { .. } => true,
PeerState::Poisoned |
PeerState::Banned { .. } => false,
}
}
/// True if there exists an established connection to the peer
/// that is open for custom protocol traffic.
fn is_open(&self) -> bool {
self.get_open().is_some()
@@ -338,6 +352,34 @@ impl GenericProto {
self.notif_protocols.push((protocol_name.into(), handshake_msg.into()));
}
/// Modifies the handshake of the given notifications protocol.
///
/// Has no effect if the protocol is unknown.
pub fn set_notif_protocol_handshake(
&mut self,
protocol_name: &[u8],
handshake_message: impl Into<Vec<u8>>
) {
let handshake_message = handshake_message.into();
if let Some(protocol) = self.notif_protocols.iter_mut().find(|(name, _)| name == &protocol_name) {
protocol.1 = handshake_message.clone();
} else {
return;
}
// Send an event to all the peers we're connected to, updating the handshake message.
for (peer_id, _) in self.peers.iter().filter(|(_, state)| state.is_connected()) {
self.events.push(NetworkBehaviourAction::NotifyHandler {
peer_id: peer_id.clone(),
handler: NotifyHandler::All,
event: NotifsHandlerIn::UpdateHandshake {
protocol_name: Cow::Owned(protocol_name.to_owned()),
handshake_message: handshake_message.clone(),
},
});
}
}
/// Returns the number of discovered nodes that we keep in memory.
pub fn num_discovered_peers(&self) -> usize {
self.peerset.num_discovered_peers()