Limit GetBlocks response size (#515)

Put a hard limit of 512 blocks in the response returned by
GetBlocks to avoid slowing things down. This number was chosen
rather arbitrarily. We might want to do some more fine tuning.
This commit is contained in:
Giacomo Pasini 2023-11-07 10:25:22 +01:00 committed by GitHub
parent a58d3fb63c
commit df683ad154
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 1 deletions

View File

@ -55,6 +55,9 @@ use overwatch_rs::services::{
};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
// Limit the number of blocks returned by GetBlocks
// Approx 64KB of data
const BLOCKS_LIMIT: usize = 512;
fn default_timeout() -> Duration {
DEFAULT_TIMEOUT
@ -441,7 +444,8 @@ where
while let Some(block) = blocks.get(&cur) {
res.push(block.clone());
if cur == to || cur == carnot.genesis_block().id {
// limit the response size
if cur == to || cur == carnot.genesis_block().id || res.len() >= BLOCKS_LIMIT {
break;
}
cur = block.parent();