mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 08:51:08 +03:00
Network block broadcasting.
This commit is contained in:
parent
c1340223de
commit
dc33ebcf39
3 changed files with 21 additions and 1 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue