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

Send list of peers before dropping a peer #2725

Closed
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion p2p/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::core::core::hash::{Hash, Hashed};
use crate::core::pow::Difficulty;
use crate::core::{core, global};
use crate::handshake::Handshake;
use crate::msg::{self, BanReason, GetPeerAddrs, Locator, Ping, TxHashSetRequest};
use crate::msg::{self, BanReason, GetPeerAddrs, Locator, PeerAddrs, Ping, TxHashSetRequest};
use crate::protocol::Protocol;
use crate::types::{
Capabilities, ChainAdapter, Error, NetAdapter, P2PConfig, PeerAddr, PeerInfo, ReasonForBan,
Expand Down Expand Up @@ -387,6 +387,11 @@ impl Peer {
)
}

pub fn send_peer_list(&self, peers: Vec<PeerAddr>) -> Result<(), Error> {
trace!("Sending {} more peers", self.info.addr);
connection!(self).send(&PeerAddrs { peers }, msg::Type::PeerAddrs)
}

pub fn send_txhashset_request(&self, height: u64, hash: Hash) -> Result<(), Error> {
debug!(
"Asking {} for txhashset archive at {} {}.",
Expand Down
22 changes: 15 additions & 7 deletions p2p/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,21 @@ impl Peers {
.saturating_sub(max_count);
if excess_count > 0 {
// map peers to addrs in a block to bound how long we keep the read lock for
let mut addrs = self
.connected_peers()
.iter()
.take(excess_count)
.map(|x| x.info.addr.clone())
.collect::<Vec<_>>();
rm.append(&mut addrs);
let peers_to_stop = self.connected_peers().into_iter().take(excess_count);
let peers = self.find_peer_addrs(Capabilities::PEER_LIST);
for peer in peers_to_stop {
match peer.send_peer_list(peers.clone()) {
Ok(_) => info!(
"preparing to drop a peer {:?}, sent the peer list",
peer.info.addr
),
Err(e) => error!(
"failed to send list of peers to {:?} before dropping the conection: {:?}",
peer.info.addr, e
),
};
rm.push(peer.info.addr);
}
}

// now clean up peer map based on the list to remove
Expand Down