From 3016d54f13b5c7e34d3d80cb1a5ae25f8180383a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Fri, 15 Jun 2018 16:30:34 +0100 Subject: [PATCH] Minor fix in chain supplier and light provider (#8906) * fix chain supplier increment * fix light provider block_headers --- ethcore/light/src/provider.rs | 9 ++++++--- ethcore/sync/src/chain/supplier.rs | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ethcore/light/src/provider.rs b/ethcore/light/src/provider.rs index e75915e8c..84e7b7283 100644 --- a/ethcore/light/src/provider.rs +++ b/ethcore/light/src/provider.rs @@ -54,6 +54,7 @@ pub trait Provider: Send + Sync { /// results within must adhere to the `skip` and `reverse` parameters. fn block_headers(&self, req: request::CompleteHeadersRequest) -> Option { use request::HashOrNumber; + const MAX_HEADERS_TO_SEND: u64 = 512; if req.max == 0 { return None } @@ -82,10 +83,12 @@ pub trait Provider: Send + Sync { } }; - let headers: Vec<_> = (0u64..req.max as u64) - .map(|x: u64| x.saturating_mul(req.skip + 1)) + let max = ::std::cmp::min(MAX_HEADERS_TO_SEND, req.max); + + let headers: Vec<_> = (0u64..max) + .map(|x: u64| x.saturating_mul(req.skip.saturating_add(1))) .take_while(|x| if req.reverse { x < &start_num } else { best_num.saturating_sub(start_num) >= *x }) - .map(|x| if req.reverse { start_num - x } else { start_num + x }) + .map(|x| if req.reverse { start_num.saturating_sub(x) } else { start_num.saturating_add(x) }) .map(|x| self.block_header(BlockId::Number(x))) .take_while(|x| x.is_some()) .flat_map(|x| x) diff --git a/ethcore/sync/src/chain/supplier.rs b/ethcore/sync/src/chain/supplier.rs index e0245fdbc..9b6efbacb 100644 --- a/ethcore/sync/src/chain/supplier.rs +++ b/ethcore/sync/src/chain/supplier.rs @@ -133,7 +133,7 @@ impl SyncSupplier { let max_count = cmp::min(MAX_HEADERS_TO_SEND, max_headers); let mut count = 0; let mut data = Bytes::new(); - let inc = (skip + 1) as BlockNumber; + let inc = skip.saturating_add(1) as BlockNumber; let overlay = io.chain_overlay().read(); // We are checking the `overlay` as well since it's where the ForkBlock @@ -155,9 +155,9 @@ impl SyncSupplier { if number <= inc || number == 0 { break; } - number -= inc; + number = number.saturating_sub(inc); } else { - number += inc; + number = number.saturating_add(inc); } } let mut rlp = RlpStream::new_list(count as usize);