feat(net): allow disconnect with reason (#519)

This commit is contained in:
Matthias Seitz
2022-12-19 16:22:24 +01:00
committed by GitHub
parent c1a863d067
commit a5f01238b9
2 changed files with 11 additions and 5 deletions

View File

@ -460,8 +460,8 @@ where
NetworkHandleMessage::AddPeerAddress(peer, addr) => {
self.swarm.state_mut().add_peer_address(peer, addr);
}
NetworkHandleMessage::DisconnectPeer(peer_id) => {
self.swarm.sessions_mut().disconnect(peer_id, None);
NetworkHandleMessage::DisconnectPeer(peer_id, reason) => {
self.swarm.sessions_mut().disconnect(peer_id, reason);
}
NetworkHandleMessage::ReputationChange(peer_id, kind) => {
self.swarm.state_mut().peers_mut().apply_reputation_change(&peer_id, kind);

View File

@ -6,7 +6,7 @@ use crate::{
FetchClient,
};
use parking_lot::Mutex;
use reth_eth_wire::{NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_interfaces::p2p::headers::client::StatusUpdater;
use reth_primitives::{PeerId, TransactionSigned, TxHash, H256, U256};
use std::{
@ -124,7 +124,13 @@ impl NetworkHandle {
/// Sends a message to the [`NetworkManager`](crate::NetworkManager) to disconnect an existing
/// connection to the given peer.
pub fn disconnect_peer(&self, peer: PeerId) {
self.send_message(NetworkHandleMessage::DisconnectPeer(peer))
self.send_message(NetworkHandleMessage::DisconnectPeer(peer, None))
}
/// Sends a message to the [`NetworkManager`](crate::NetworkManager) to disconnect an existing
/// connection to the given peer using the provided reason
pub fn disconnect_peer_with_reason(&self, peer: PeerId, reason: DisconnectReason) {
self.send_message(NetworkHandleMessage::DisconnectPeer(peer, Some(reason)))
}
/// Send a reputation change for the given peer.
@ -183,7 +189,7 @@ pub(crate) enum NetworkHandleMessage {
/// Adds an address for a peer.
AddPeerAddress(PeerId, SocketAddr),
/// Disconnect a connection to a peer if it exists.
DisconnectPeer(PeerId),
DisconnectPeer(PeerId, Option<DisconnectReason>),
/// Add a new listener for [`NetworkEvent`].
EventListener(UnboundedSender<NetworkEvent>),
/// Broadcast event to announce a new block to all nodes.