pub fn needed_blocks(&mut self, who: PeerId, count: usize, peer_best: NumberFor<B>, common: NumberFor<B>) -> Option<Range<NumberFor<B>>> {
		// First block number that we need to download
		let first_different = common + As::sa(1);
		let count = As::sa(count as u64);
		let (mut range, downloading) = {
			let mut downloading_iter = self.blocks.iter().peekable();
			let mut prev: Option<(&NumberFor<B>, &BlockRangeState<B>)> = None;
			loop {
				let next = downloading_iter.next();
				break match &(prev, next) {
					&(Some((start, &BlockRangeState::Downloading { ref len, downloading })), _) if downloading < MAX_PARALLEL_DOWNLOADS =>
						(*start .. *start + *len, downloading),
					&(Some((start, r)), Some((next_start, _))) if *start + r.len() < *next_start =>
						(*start + r.len() .. cmp::min(*next_start, *start + r.len() + count), 0), // gap
					&(Some((start, r)), None) =>
						(*start + r.len() .. *start + r.len() + count, 0), // last range
					&(None, None) =>
						(first_different .. first_different + count, 0), // empty
					&(None, Some((start, _))) if *start > first_different =>
						(first_different .. cmp::min(first_different + count, *start), 0), // gap at the start
					_ => {
						prev = next;
						continue
					},
				}
			}
		};
		// crop to peers best
		if range.start > peer_best {
			trace!(target: "sync", "Out of range for peer {} ({} vs {})", who, range.start, peer_best);
			return None;
		}
		range.end = cmp::min(peer_best + As::sa(1), range.end);
		self.peer_requests.insert(who, range.start);
		self.blocks.insert(range.start, BlockRangeState::Downloading { len: range.end - range.start, downloading: downloading + 1 });
		if range.end <= range.start {
			panic!("Empty range {:?}, count={}, peer_best={}, common={}, blocks={:?}", range, count, peer_best, common, self.blocks);
		}
		Some(range)
	}
/// Returns a set of block hashes that require a header download. The returned set is marked as being downloaded.
Returns a set of block hashes that require a header download. The returned set is marked as being downloaded.
Returns a set of block hashes that require a header download . The returned set is marked as being downloaded .
pub fn needed_blocks(&mut self, who: PeerId, count: usize, peer_best: NumberFor<B>, common: NumberFor<B>) -> Option<Range<NumberFor<B>>> {
		// First block number that we need to download
		let first_different = common + As::sa(1);
		let count = As::sa(count as u64);
		let (mut range, downloading) = {
			let mut downloading_iter = self.blocks.iter().peekable();
			let mut prev: Option<(&NumberFor<B>, &BlockRangeState<B>)> = None;
			loop {
				let next = downloading_iter.next();
				break match &(prev, next) {
					&(Some((start, &BlockRangeState::Downloading { ref len, downloading })), _) if downloading < MAX_PARALLEL_DOWNLOADS =>
						(*start .. *start + *len, downloading),
					&(Some((start, r)), Some((next_start, _))) if *start + r.len() < *next_start =>
						(*start + r.len() .. cmp::min(*next_start, *start + r.len() + count), 0), // gap
					&(Some((start, r)), None) =>
						(*start + r.len() .. *start + r.len() + count, 0), // last range
					&(None, None) =>
						(first_different .. first_different + count, 0), // empty
					&(None, Some((start, _))) if *start > first_different =>
						(first_different .. cmp::min(first_different + count, *start), 0), // gap at the start
					_ => {
						prev = next;
						continue
					},
				}
			}
		};
		// crop to peers best
		if range.start > peer_best {
			trace!(target: "sync", "Out of range for peer {} ({} vs {})", who, range.start, peer_best);
			return None;
		}
		range.end = cmp::min(peer_best + As::sa(1), range.end);
		self.peer_requests.insert(who, range.start);
		self.blocks.insert(range.start, BlockRangeState::Downloading { len: range.end - range.start, downloading: downloading + 1 });
		if range.end <= range.start {
			panic!("Empty range {:?}, count={}, peer_best={}, common={}, blocks={:?}", range, count, peer_best, common, self.blocks);
		}
		Some(range)
	}
pub fn needed_blocks ( & mut self , who : PeerId , count : usize , peer_best : NumberFor < B > , common : NumberFor < B > ) -> Option < Range < NumberFor < B > > > { // First block number that we need to download let first_different = common + As :: sa ( 1 ) ; let count = As :: sa ( count as u64 ) ; let ( mut range , downloading ) = { let mut downloading_iter = self . blocks . iter ( ) . peekable ( ) ; let mut prev : Option < ( & NumberFor < B > , & BlockRangeState < B > ) > = None ; loop { let next = downloading_iter . next ( ) ; break match & ( prev , next ) { & ( Some ( ( start , & BlockRangeState :: Downloading { ref len , downloading } ) ) , _ ) if downloading < MAX_PARALLEL_DOWNLOADS => ( * start .. * start + * len , downloading ) , & ( Some ( ( start , r ) ) , Some ( ( next_start , _ ) ) ) if * start + r . len ( ) < * next_start => ( * start + r . len ( ) .. cmp :: min ( * next_start , * start + r . len ( ) + count ) , 0 ) , // gap & ( Some ( ( start , r ) ) , None ) => ( * start + r . len ( ) .. * start + r . len ( ) + count , 0 ) , // last range & ( None , None ) => ( first_different .. first_different + count , 0 ) , // empty & ( None , Some ( ( start , _ ) ) ) if * start > first_different => ( first_different .. cmp :: min ( first_different + count , * start ) , 0 ) , // gap at the start _ => { prev = next ; continue } , } } } ; // crop to peers best if range . start > peer_best { trace ! ( target " " " " who range start peer_best ) ; return None ; } range . end = cmp :: min ( peer_best + As :: sa ( 1 ) , range . end ) ; self . peer_requests . insert ( who , range . start ) ; self . blocks . insert ( range . start , BlockRangeState :: Downloading { len : range . end - range . start , downloading : downloading + 1 } ) ; if range . end <= range . start { panic ! ( " " range count peer_best common self blocks ) ; } Some ( range ) }
Returns a set of block hashes that require a header download.
Returns a set of block hashes that require a header download .
&mut self
who
PeerId
count
usize
peer_best
NumberFor<B>
common
NumberFor<B>
&mut self
who
PeerId
count
usize
peer_best
NumberFor<B>
common
NumberFor<B>