Skip to content

Commit

Permalink
Merge pull request #2953 from sqrrm/limit-block-req-size
Browse files Browse the repository at this point in the history
Limit number of blocks sent in GetBlocksResponse
  • Loading branch information
ripcurlx authored Jul 12, 2019
2 parents 3b915a3 + a0104c0 commit c45d4a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public GetBlocksRequestHandler(NetworkNode networkNode, DaoStateService daoState
///////////////////////////////////////////////////////////////////////////////////////////

public void onGetBlocksRequest(GetBlocksRequest getBlocksRequest, final Connection connection) {
List<Block> blocks = new LinkedList<>(daoStateService.getBlocksFromBlockHeight(getBlocksRequest.getFromBlockHeight()));
// We limit number of blocks to 6000 which is about 1.5 month.
List<Block> blocks = new LinkedList<>(daoStateService.getBlocksFromBlockHeight(getBlocksRequest.getFromBlockHeight(), 6000));
List<RawBlock> rawBlocks = blocks.stream().map(RawBlock::fromBlock).collect(Collectors.toList());
GetBlocksResponse getBlocksResponse = new GetBlocksResponse(rawBlocks, getBlocksRequest.getNonce());
log.info("Received GetBlocksRequest from {} for blocks from height {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@EqualsAndHashCode(callSuper = true)
@Getter
@Slf4j
public final class GetBlocksResponse extends NetworkEnvelope implements DirectMessage, ExtendedDataSizePermission {
private final List<RawBlock> blocks;
private final int requestNonce;
Expand Down Expand Up @@ -67,11 +69,13 @@ public PB.NetworkEnvelope toProtoNetworkEnvelope() {
}

public static NetworkEnvelope fromProto(PB.GetBlocksResponse proto, int messageVersion) {
List<RawBlock> list = proto.getRawBlocksList().stream()
.map(RawBlock::fromProto)
.collect(Collectors.toList());
log.info("Received a GetBlocksResponse with {} blocks and {} kB size", list.size(), proto.getSerializedSize() / 1000d);
return new GetBlocksResponse(proto.getRawBlocksList().isEmpty() ?
new ArrayList<>() :
proto.getRawBlocksList().stream()
.map(RawBlock::fromProto)
.collect(Collectors.toList()),
list,
proto.getRequestNonce(),
messageVersion);
}
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/bisq/core/dao/state/DaoStateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,13 @@ public long getBlockTime(int height) {
return getBlockAtHeight(height).map(Block::getTime).orElse(0L);
}

public List<Block> getBlocksFromBlockHeight(int fromBlockHeight) {
public List<Block> getBlocksFromBlockHeight(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)
.sorted(Comparator.comparing(Block::getHeight))
.limit(numMaxBlocks)
.collect(Collectors.toList());
}

Expand Down

0 comments on commit c45d4a7

Please sign in to comment.