Minor fix in chain supplier and light provider (#8906)

* fix chain supplier increment

* fix light provider block_headers
This commit is contained in:
André Silva 2018-06-15 16:30:34 +01:00 committed by Robert Habermeier
parent 05e7c133fb
commit 3016d54f13
2 changed files with 9 additions and 6 deletions

View File

@ -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<request::HeadersResponse> {
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)

View File

@ -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);