Network block broadcasting.

This commit is contained in:
Ignotus Peverell 2016-12-20 17:33:20 -08:00
parent c1340223de
commit dc33ebcf39
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
3 changed files with 21 additions and 1 deletions

View file

@ -72,6 +72,13 @@ impl Peer {
self.proto.send_ping()
}
/// Sends the provided block to the remote peer. The request may be dropped
/// if the remote peer is known to already have the block.
pub fn send_block(&self, b: &core::Block) -> Result<(), Error> {
// TODO do not send if the peer sent us the block in the first place
self.proto.send_block(b)
}
pub fn stop(&self) {
self.proto.close();
}

View file

@ -139,6 +139,18 @@ impl Server {
Box::new(request)
}
/// Broadcasts the provided block to all our 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 peers = self.peers.write().unwrap();
for p in peers.deref() {
if let Err(e) = p.send_block(b) {
debug!("Error sending block to peer: {}", e);
}
}
}
pub fn peers_count(&self) -> u32 {
self.peers.read().unwrap().len() as u32
}

View file

@ -88,7 +88,8 @@ pub trait Protocol {
}
/// Bridge between the networking layer and the rest of the system. Handles the
/// forwarding or querying of blocks and transactions among other things.
/// forwarding or querying of blocks and transactions from the network among
/// other things.
pub trait NetAdapter {
/// A valid transaction has been received from one of our peers
fn transaction_received(&self, tx: core::Transaction);