Network block broadcasting.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+2
-1
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user