Skip to content

Commit

Permalink
Merge pull request #6995 from alvasw/on_get_blocks_request_handling_c…
Browse files Browse the repository at this point in the history
…reate_raw_block_list_once

Create GetBlocksRequestHandler's reply list once
  • Loading branch information
alejandrogarcia83 authored Jan 8, 2024
2 parents 172628f + 1e78902 commit 566ab53
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 566ab53

Please sign in to comment.