Peers broadcast fixes (#1667)

* peers broadcast fixes
* stop at num_peers...
This commit is contained in:
Antioch Peverell 2018-10-06 00:53:55 +01:00 committed by Ignotus Peverell
parent 7f2e2158c8
commit c86578c2a9

View file

@ -282,33 +282,44 @@ impl Peers {
}; };
} }
fn broadcast<F>(&self, obj_name: &str, f: F) -> u8 fn broadcast<F>(&self, obj_name: &str, num_peers: u32, f: F) -> u32
where where
F: Fn(&Peer) -> Result<(), Error>, F: Fn(&Peer) -> Result<(), Error>,
{ {
let peers = self.connected_peers(); let peers = self.connected_peers();
let preferred_peers = 8;
let mut count = 0; let mut count = 0;
for p in peers.iter().take(preferred_peers) {
let p = p.read().unwrap(); // Iterate over our connected peers.
if p.is_connected() { // Try our best to send to at most num_peers peers.
if let Err(e) = f(&p) { for p in peers.iter() {
debug!(LOGGER, "Error sending {} to peer: {:?}", obj_name, e); match p.try_read() {
} else { Ok(p) => {
count += 1; if p.is_connected() {
if let Err(e) = f(&p) {
debug!(LOGGER, "Error sending {} to peer: {:?}", obj_name, e);
} else {
count += 1;
}
}
} }
Err(_) => (),
}
if count >= num_peers {
break;
} }
} }
count count
} }
/// Broadcasts the provided compact block to PEER_PREFERRED_COUNT of our peers. /// Broadcasts the provided compact block to PEER_MAX_COUNT of our peers.
/// We may be connected to PEER_MAX_COUNT peers so we only /// This is only used when initially broadcasting a newly mined block
/// want to broadcast to a random subset of peers. /// from a mining node so we want to broadcast it far and wide.
/// A peer implementation may drop the broadcast request /// A peer implementation may drop the broadcast request
/// if it knows the remote peer already has the block. /// if it knows the remote peer already has the block.
pub fn broadcast_compact_block(&self, b: &core::CompactBlock) { pub fn broadcast_compact_block(&self, b: &core::CompactBlock) {
let count = self.broadcast("compact block", |p| p.send_compact_block(b)); let num_peers = self.config.peer_max_count();
let count = self.broadcast("compact block", num_peers, |p| p.send_compact_block(b));
debug!( debug!(
LOGGER, LOGGER,
"broadcast_compact_block: {}, {} at {}, to {} peers, done.", "broadcast_compact_block: {}, {} at {}, to {} peers, done.",
@ -325,8 +336,9 @@ impl Peers {
/// A peer implementation may drop the broadcast request /// A peer implementation may drop the broadcast request
/// if it knows the remote peer already has the header. /// if it knows the remote peer already has the header.
pub fn broadcast_header(&self, bh: &core::BlockHeader) { pub fn broadcast_header(&self, bh: &core::BlockHeader) {
let count = self.broadcast("header", |p| p.send_header(bh)); let num_peers = self.config.peer_min_preferred_count();
trace!( let count = self.broadcast("header", num_peers, |p| p.send_header(bh));
debug!(
LOGGER, LOGGER,
"broadcast_header: {}, {} at {}, to {} peers, done.", "broadcast_header: {}, {} at {}, to {} peers, done.",
bh.hash(), bh.hash(),
@ -368,7 +380,8 @@ impl Peers {
/// A peer implementation may drop the broadcast request /// A peer implementation may drop the broadcast request
/// if it knows the remote peer already has the transaction. /// if it knows the remote peer already has the transaction.
pub fn broadcast_transaction(&self, tx: &core::Transaction) { pub fn broadcast_transaction(&self, tx: &core::Transaction) {
let count = self.broadcast("transaction", |p| p.send_transaction(tx)); let num_peers = self.config.peer_min_preferred_count();
let count = self.broadcast("transaction", num_peers, |p| p.send_transaction(tx));
trace!( trace!(
LOGGER, LOGGER,
"broadcast_transaction: {}, to {} peers, done.", "broadcast_transaction: {}, to {} peers, done.",