Class BlockFetcher

java.lang.Object
com.privatechain.network.sync.BlockFetcher

public final class BlockFetcher extends Object
Fetches missing blocks from a specific remote peer during chain synchronization.

BlockFetcher is used by SyncManager to download blocks that the local node is missing. It sends GET_BLOCKS requests to a chosen peer and returns the decoded block list for validation and persistence.

Batch size

Block fetching is chunked into batches of at most BATCH_SIZE blocks to avoid creating excessively large TCP messages. For large sync gaps the fetcher issues multiple requests until all blocks are obtained.

Thread safety

Instances are stateless and safe for concurrent use once constructed.

Since:
1.0.0
See Also:
  • Field Details

    • BATCH_SIZE

      public static final int BATCH_SIZE
      Maximum number of blocks requested in a single GET_BLOCKS message.
      See Also:
  • Constructor Details

    • BlockFetcher

      public BlockFetcher(String localNodeId, NodeClient nodeClient, MessageCodec codec)
      Constructs a BlockFetcher.
      Parameters:
      localNodeId - this node's stable logical identifier (non-null, non-blank)
      nodeClient - outbound connection client (non-null)
      codec - message codec (non-null)
      Throws:
      NullPointerException - if any parameter is null
      IllegalArgumentException - if localNodeId is blank
  • Method Details

    • fetchBlocks

      public List<Block> fetchBlocks(Peer peer, int fromHeight, int toHeight)
      Fetches all blocks from fromHeight (inclusive) to toHeight (inclusive) from the given peer, in ascending order.

      Blocks are fetched in batches of at most BATCH_SIZE. If any batch fails to download, the method returns whatever blocks were collected before the failure (partial result). The caller (SyncManager) is responsible for detecting gaps and retrying if needed.

      Parameters:
      peer - the peer to fetch from (non-null)
      fromHeight - the first block height to request (≥ 0)
      toHeight - the last block height to request (≥ fromHeight)
      Returns:
      ordered list of fetched blocks; empty if the peer does not respond
      Throws:
      NullPointerException - if peer is null
      IllegalArgumentException - if fromHeight > toHeight