Skip to content

Commit

Permalink
Create GetBlocksRequestHandler's reply list once
Browse files Browse the repository at this point in the history
Previously, the onGetBlocksRequest method created an ArrayList and two
LinkedList before creating the GetBlocksResponse. The first two lists
were never used. PR bisq-network#6947 introduced the second LinkedList creation.
With this change, the GetBlocksRequestHandler only creates a single
LinkedList.
  • Loading branch information
alvasw committed Jan 6, 2024
1 parent dfb5039 commit 1e78902
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ public GetBlocksRequestHandler(NetworkNode networkNode, DaoStateService daoState
public void onGetBlocksRequest(GetBlocksRequest getBlocksRequest, Connection connection) {
long ts = System.currentTimeMillis();
// We limit number of blocks to 3000 which is about 3 weeks and about 5 MB on data
List<Block> blocks = daoStateService.getBlocksFromBlockHeight(getBlocksRequest.getFromBlockHeight());
List<RawBlock> rawBlocks = new LinkedList<>(blocks).stream()
List<RawBlock> rawBlocks = daoStateService.getBlocksFromBlockHeightStream(getBlocksRequest.getFromBlockHeight(), 3000)
.map(RawBlock::fromBlock)
.limit(3000)
.collect(Collectors.toList());
.collect(Collectors.toCollection(LinkedList::new));

GetBlocksResponse getBlocksResponse = new GetBlocksResponse(rawBlocks, getBlocksRequest.getNonce());
log.info("Received GetBlocksRequest from {} for blocks from height {}. " +
"Building GetBlocksResponse with {} blocks took {} ms.",
Expand Down
14 changes: 12 additions & 2 deletions core/src/main/java/bisq/core/dao/state/DaoStateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,22 @@ public long getBlockTime(int height) {
}

public List<Block> getBlocksFromBlockHeight(int fromBlockHeight) {
return getBlocksFromBlockHeight(fromBlockHeight, Integer.MAX_VALUE);
}

public List<Block> getBlocksFromBlockHeight(int fromBlockHeight, int numMaxBlocks) {
return getBlocksFromBlockHeightStream(fromBlockHeight, numMaxBlocks)
.collect(Collectors.toCollection(LinkedList::new));
}

public Stream<Block> getBlocksFromBlockHeightStream(int fromBlockHeight, int numMaxBlocks) {
// We limit requests to numMaxBlocks blocks, to avoid performance issues and too
// large network data in case a node requests too far back in history.
return getBlocks().stream()
.filter(block -> block.getHeight() >= fromBlockHeight)
.collect(Collectors.toList());
.limit(numMaxBlocks);
}


///////////////////////////////////////////////////////////////////////////////////////////
// Genesis
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 1e78902

Please sign in to comment.