Class BlockBroadcaster

java.lang.Object
com.privatechain.network.gossip.BlockBroadcaster
All Implemented Interfaces:
BlockchainEventListener

public final class BlockBroadcaster extends Object implements BlockchainEventListener
Propagates newly mined or locally accepted blocks to all connected peers.

BlockBroadcaster implements BlockchainEventListener so that it can be registered on the BlockchainEventBus. Whenever a BlockchainEvent.BlockAddedEvent is published — indicating that a block has been appended to the local canonical chain — this class fans the block out to every connected peer (FR-NET-02).

Fan-out strategy

Unlike GossipProtocol (which sends to a random subset for transaction propagation), the broadcaster sends to all connected peers. Blocks are critical consensus data that every peer must receive promptly.

Deduplication

Each outbound broadcast records the sending node's ID in the message envelope (via the MessageCodec.blockMessage(java.lang.String, com.privatechain.core.model.Block) factory). Remote nodes that receive the block will propagate it further via their own event listeners, but because the senderId is echoed back, each node can avoid re-broadcasting a block it received from the network (loop prevention at the application layer).

Thread safety

Broadcasts are submitted to a cached thread pool, so multiple blocks can be fanned out concurrently without blocking the event bus delivery thread.

Since:
1.0.0
See Also:
  • Constructor Details

    • BlockBroadcaster

      public BlockBroadcaster(String localNodeId, PeerManager peerManager, NodeClient nodeClient, MessageCodec codec)
      Constructs a BlockBroadcaster.
      Parameters:
      localNodeId - this node's stable logical identifier (non-null, non-blank)
      peerManager - the peer manager for obtaining the connected-peer list (non-null)
      nodeClient - the outbound connection client (non-null)
      codec - the message codec (non-null)
      Throws:
      NullPointerException - if any parameter is null
      IllegalArgumentException - if localNodeId is blank
  • Method Details

    • onEvent

      public void onEvent(BlockchainEvent event)
      Reacts to blockchain events, broadcasting new blocks to all connected peers.

      Only BlockchainEvent.BlockAddedEvent instances trigger a broadcast; all other event types are ignored silently.

      Specified by:
      onEvent in interface BlockchainEventListener
      Parameters:
      event - the blockchain event to handle (non-null)
    • broadcast

      public void broadcast(Block block)
      Fans out the given block to all currently connected peers.

      Each peer receives its own message on an independent thread so that a slow or unresponsive peer cannot stall delivery to the others. The broadcast pool is daemon-threaded, so in-flight sends are aborted on JVM exit.

      Parameters:
      block - the block to broadcast (non-null)
      Throws:
      NullPointerException - if block is null
    • shutdown

      public void shutdown()
      Shuts down the broadcast thread pool.

      In-flight broadcasts that have already been submitted will complete. New broadcasts submitted after this call will be rejected.