Simplify block broadcast (#1549)

* removed random "block vs compact_block"
This commit is contained in:
Antioch Peverell 2018-09-18 18:51:37 +01:00 committed by Ignotus Peverell
parent a4476443bb
commit c291c48436
3 changed files with 15 additions and 77 deletions

View file

@ -130,8 +130,7 @@ impl Peers {
.filter(|x| match x.try_read() { .filter(|x| match x.try_read() {
Ok(peer) => peer.info.direction == Direction::Outbound, Ok(peer) => peer.info.direction == Direction::Outbound,
Err(_) => false, Err(_) => false,
}) }).collect::<Vec<_>>();
.collect::<Vec<_>>();
res res
} }
@ -165,8 +164,7 @@ impl Peers {
.filter(|x| match x.try_read() { .filter(|x| match x.try_read() {
Ok(peer) => peer.info.total_difficulty > total_difficulty, Ok(peer) => peer.info.total_difficulty > total_difficulty,
Err(_) => false, Err(_) => false,
}) }).collect::<Vec<_>>();
.collect::<Vec<_>>();
thread_rng().shuffle(&mut max_peers); thread_rng().shuffle(&mut max_peers);
max_peers max_peers
@ -190,8 +188,7 @@ impl Peers {
&& peer.info.capabilities.contains(Capabilities::FULL_HIST) && peer.info.capabilities.contains(Capabilities::FULL_HIST)
} }
Err(_) => false, Err(_) => false,
}) }).collect::<Vec<_>>();
.collect::<Vec<_>>();
thread_rng().shuffle(&mut max_peers); thread_rng().shuffle(&mut max_peers);
max_peers max_peers
@ -220,8 +217,7 @@ impl Peers {
.map(|x| match x.try_read() { .map(|x| match x.try_read() {
Ok(peer) => peer.info.total_difficulty.clone(), Ok(peer) => peer.info.total_difficulty.clone(),
Err(_) => Difficulty::zero(), Err(_) => Difficulty::zero(),
}) }).max()
.max()
.unwrap(); .unwrap();
let mut max_peers = peers let mut max_peers = peers
@ -229,8 +225,7 @@ impl Peers {
.filter(|x| match x.try_read() { .filter(|x| match x.try_read() {
Ok(peer) => peer.info.total_difficulty == max_total_difficulty, Ok(peer) => peer.info.total_difficulty == max_total_difficulty,
Err(_) => false, Err(_) => false,
}) }).collect::<Vec<_>>();
.collect::<Vec<_>>();
thread_rng().shuffle(&mut max_peers); thread_rng().shuffle(&mut max_peers);
max_peers max_peers
@ -303,23 +298,6 @@ impl Peers {
count count
} }
/// Broadcasts the provided block to PEER_PREFERRED_COUNT of our peers.
/// We may be connected to PEER_MAX_COUNT peers so we only
/// want to broadcast to a random subset of peers.
/// A peer implementation may drop the broadcast request
/// if it knows the remote peer already has the block.
pub fn broadcast_block(&self, b: &core::Block) {
let count = self.broadcast("block", |p| p.send_block(b));
debug!(
LOGGER,
"broadcast_block: {} @ {} [{}] was sent to {} peers.",
b.header.pow.total_difficulty,
b.header.height,
b.hash(),
count,
);
}
/// Broadcasts the provided compact block to PEER_PREFERRED_COUNT of our peers. /// Broadcasts the provided compact block to PEER_PREFERRED_COUNT of our peers.
/// We may be connected to PEER_MAX_COUNT peers so we only /// We may be connected to PEER_MAX_COUNT peers so we only
/// want to broadcast to a random subset of peers. /// want to broadcast to a random subset of peers.
@ -489,8 +467,7 @@ impl Peers {
.map(|x| { .map(|x| {
let p = x.read().unwrap(); let p = x.read().unwrap();
p.info.addr.clone() p.info.addr.clone()
}) }).collect::<Vec<_>>()
.collect::<Vec<_>>()
}; };
// now remove them taking a short-lived write lock each time // now remove them taking a short-lived write lock each time

View file

@ -27,7 +27,6 @@ use msg::{
read_exact, BanReason, GetPeerAddrs, Headers, Locator, PeerAddrs, Ping, Pong, SockAddr, read_exact, BanReason, GetPeerAddrs, Headers, Locator, PeerAddrs, Ping, Pong, SockAddr,
TxHashSetArchive, TxHashSetRequest, Type, TxHashSetArchive, TxHashSetRequest, Type,
}; };
use rand::{self, Rng};
use types::{Error, NetAdapter}; use types::{Error, NetAdapter};
use util::LOGGER; use util::LOGGER;
@ -107,7 +106,10 @@ impl MessageHandler for Protocol {
Type::GetBlock => { Type::GetBlock => {
let h: Hash = msg.body()?; let h: Hash = msg.body()?;
trace!(LOGGER, "handle_payload: GetBlock {}", h); debug!(
LOGGER,
"handle_payload: Getblock: {}, msg_len: {}", h, msg.header.msg_len,
);
let bo = adapter.get_block(h); let bo = adapter.get_block(h);
if let Some(b) = bo { if let Some(b) = bo {
@ -129,25 +131,9 @@ impl MessageHandler for Protocol {
Type::GetCompactBlock => { Type::GetCompactBlock => {
let h: Hash = msg.body()?; let h: Hash = msg.body()?;
if let Some(b) = adapter.get_block(h) { if let Some(b) = adapter.get_block(h) {
// if we have txs in the block send a compact block let cb: CompactBlock = b.into();
// but if block is empty - Ok(Some(msg.respond(Type::CompactBlock, cb)))
// to allow us to test all code paths, randomly choose to send
// either the block or the compact block
let mut rng = rand::thread_rng();
if b.kernels().len() == 1 && rng.gen() {
debug!(
LOGGER,
"handle_payload: GetCompactBlock: empty block, sending full block",
);
Ok(Some(msg.respond(Type::Block, b)))
} else {
let cb: CompactBlock = b.into();
Ok(Some(msg.respond(Type::CompactBlock, cb)))
}
} else { } else {
Ok(None) Ok(None)
} }

View file

@ -15,7 +15,6 @@
//! Adapters connecting new block, new transaction, and accepted transaction //! Adapters connecting new block, new transaction, and accepted transaction
//! events to consumers of those events. //! events to consumers of those events.
use rand::{self, Rng};
use std::fs::File; use std::fs::File;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::ops::Deref; use std::ops::Deref;
@ -643,40 +642,16 @@ impl ChainAdapter for ChainToPoolAndNetAdapter {
); );
} }
// If we mined the block then we want to broadcast the block itself. // If we mined the block then we want to broadcast the compact block.
// If block is empty then broadcast the block.
// If block contains txs then broadcast the compact block.
// If we received the block from another node then broadcast "header first" // If we received the block from another node then broadcast "header first"
// to minimize network traffic. // to minimize network traffic.
if opts.contains(Options::MINE) { if opts.contains(Options::MINE) {
// propagate compact block out if we mined the block // propagate compact block out if we mined the block
// but broadcast full block if we have no txs
let cb: CompactBlock = b.clone().into(); let cb: CompactBlock = b.clone().into();
if cb.kern_ids().is_empty() { wo(&self.peers).broadcast_compact_block(&cb);
// In the interest of exercising all code paths
// randomly decide how we send an empty block out.
let mut rng = rand::thread_rng();
if rng.gen() {
wo(&self.peers).broadcast_block(&b);
} else {
wo(&self.peers).broadcast_compact_block(&cb);
}
} else {
wo(&self.peers).broadcast_compact_block(&cb);
}
} else { } else {
// "header first" propagation if we are not the originator of this block // "header first" propagation if we are not the originator of this block
// again randomly chose between "header first" or "compact block" propagation wo(&self.peers).broadcast_header(&b.header);
// to ensure we test a wide variety of code paths
let mut rng = rand::thread_rng();
if rng.gen() {
wo(&self.peers).broadcast_header(&b.header);
} else {
let cb = b.clone().into();
wo(&self.peers).broadcast_compact_block(&cb);
}
} }
} }
} }