From 9a5892e187f7d9b3f058b549ad5859793d117d7b Mon Sep 17 00:00:00 2001 From: arkpar Date: Thu, 18 Jun 2020 10:39:54 +0200 Subject: [PATCH] Block packet size limit --- client/network/src/protocol.rs | 8 +++++++- client/network/src/protocol/sync.rs | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index 764c416495464..6e08215050ed9 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -92,6 +92,10 @@ pub(crate) const MIN_VERSION: u32 = 3; // Maximum allowed entries in `BlockResponse` const MAX_BLOCK_DATA_RESPONSE: u32 = 128; +// Maximum total bytes allowed for block bodies in `BlockResponse` +// TODO: increase this to 4Mb once yamux limit is increased +const MAX_BODIES_BYTES: usize = 1 * 1024 * 1024; + /// When light node connects to the full node and the full node is behind light node /// for at least `LIGHT_MAXIMAL_BLOCKS_DIFFERENCE` blocks, we consider it not useful /// and disconnect to free connection slot. @@ -762,8 +766,9 @@ impl Protocol { let get_justification = request .fields .contains(message::BlockAttributes::JUSTIFICATION); + let mut total_size = 0; while let Some(header) = self.context_data.chain.header(id).unwrap_or(None) { - if blocks.len() >= max { + if blocks.len() >= max || total_size > MAX_BODIES_BYTES { break; } let number = *header.number(); @@ -794,6 +799,7 @@ impl Protocol { trace!(target: "sync", "Missing data for block request."); break; } + total_size += block_data.body.as_ref().map_or(0, |b| b.len()); blocks.push(block_data); match request.direction { message::Direction::Ascending => id = BlockId::Number(number + One::one()), diff --git a/client/network/src/protocol/sync.rs b/client/network/src/protocol/sync.rs index 781d410fff993..453d3f6f04e15 100644 --- a/client/network/src/protocol/sync.rs +++ b/client/network/src/protocol/sync.rs @@ -54,7 +54,8 @@ mod blocks; mod extra_requests; /// Maximum blocks to request in a single packet. -const MAX_BLOCKS_TO_REQUEST: usize = 128; +/// TODO: set to 128 once yamux issue is resolved. +const MAX_BLOCKS_TO_REQUEST: usize = 64; /// Maximum blocks to store in the import queue. const MAX_IMPORTING_BLOCKS: usize = 2048;