Package com.privatechain.network.sync
Class BlockFetcher
java.lang.Object
com.privatechain.network.sync.BlockFetcher
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of blocks requested in a single GET_BLOCKS message. -
Constructor Summary
ConstructorsConstructorDescriptionBlockFetcher(String localNodeId, NodeClient nodeClient, MessageCodec codec) Constructs aBlockFetcher. -
Method Summary
Modifier and TypeMethodDescriptionfetchBlocks(Peer peer, int fromHeight, int toHeight) Fetches all blocks fromfromHeight(inclusive) totoHeight(inclusive) from the given peer, in ascending order.
-
Field Details
-
BATCH_SIZE
public static final int BATCH_SIZEMaximum number of blocks requested in a single GET_BLOCKS message.- See Also:
-
-
Constructor Details
-
BlockFetcher
Constructs aBlockFetcher.- 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 nullIllegalArgumentException- if localNodeId is blank
-
-
Method Details
-
fetchBlocks
Fetches all blocks fromfromHeight(inclusive) totoHeight(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 nullIllegalArgumentException- if fromHeight > toHeight
-