Repository: paritytech/substrate
File: client/network/sync/src/engine.rs (Rust)

Code before:
		while let Poll::Ready(()) = self.tick_timeout.poll_unpin(cx) {
			self.report_metrics();
			self.tick_timeout.reset(TICK_TIMEOUT);

			// if `SyncingEngine` has just started, don't evict seemingly inactive peers right away
			// as they may not have produced blocks not because they've disconnected but because
			// they're still waiting to receive enough relaychain blocks to start producing blocks.
			if let Some(started) = self.syncing_started {
				if started.elapsed() < INITIAL_EVICTION_WAIT_PERIOD {
					continue
				}

				self.syncing_started = None;
				self.last_notification_sent = Instant::now();
				self.last_notification_received = Instant::now();
			}

			// if syncing hasn't sent or received any blocks within `INACTIVITY_EVICT_THRESHOLD`,
			// it means the local node has stalled and is connected to peers who either don't
			// consider it connected or are also all stalled. In order to unstall the node,
			// disconnect all peers and allow `ProtocolController` to establish new connections.
			let last_received_late =
				self.last_notification_received.elapsed() > INACTIVITY_EVICT_THRESHOLD;
			let last_sent_late = self.last_notification_sent.elapsed() > INACTIVITY_EVICT_THRESHOLD;

			if last_received_late && last_sent_late {
				log::debug!(target: "sync", "syncing has halted due to inactivity, evicting all peers");

				for peer in self.peers.keys() {
					self.network_service.report_peer(*peer, rep::INACTIVE_SUBSTREAM);
					self.network_service
						.disconnect_peer(*peer, self.block_announce_protocol_name.clone());
				}

				// after all the peers have been evicted, start timers again to prevent evicting
				// new peers that join after the old peer have been evicted
				self.last_notification_received = Instant::now();
				self.last_notification_sent = Instant::now();
			}
		}

		while let Poll::Ready(Some(event)) = self.service_rx.poll_next_unpin(cx) {
			match event {
				ToServiceCommand::SetSyncForkRequest(peers, hash, number) => {
					self.chain_sync.set_sync_fork_request(peers, &hash, number);
				},
				ToServiceCommand::EventStream(tx) => self.event_streams.push(tx),
				ToServiceCommand::RequestJustification(hash, number) =>
					self.chain_sync.request_justification(&hash, number),
				ToServiceCommand::ClearJustificationRequests =>
					self.chain_sync.clear_justification_requests(),

Review comment (suggestion):
> if syncing hasn't sent **or** received any blocks`

There should be `||` instead of `&&`. And because of `||` there is no need to use two variables last_notification_received and last_notification_sent, its enough to use only one - something like last_notification_io.

Code after:
			// if `SyncingEngine` has just started, don't evict seemingly inactive peers right away
			// as they may not have produced blocks not because they've disconnected but because
			// they're still waiting to receive enough relaychain blocks to start producing blocks.
			if let Some(started) = self.syncing_started {
				if started.elapsed() < INITIAL_EVICTION_WAIT_PERIOD {
					continue
				}

				self.syncing_started = None;
				self.last_notification_io = Instant::now();
			}

			// if syncing hasn't sent or received any blocks within `INACTIVITY_EVICT_THRESHOLD`,
			// it means the local node has stalled and is connected to peers who either don't
			// consider it connected or are also all stalled. In order to unstall the node,
			// disconnect all peers and allow `ProtocolController` to establish new connections.
			if self.last_notification_io.elapsed() > INACTIVITY_EVICT_THRESHOLD {
				log::debug!(target: "sync", "syncing has halted due to inactivity, evicting all peers");

				for peer in self.peers.keys() {
					self.network_service.report_peer(*peer, rep::INACTIVE_SUBSTREAM);
					self.network_service
						.disconnect_peer(*peer, self.block_announce_protocol_name.clone());
				}

				// after all the peers have been evicted, start timer again to prevent evicting
				// new peers that join after the old peer have been evicted
				self.last_notification_io = Instant::now();
			}
		}

		while let Poll::Ready(Some(event)) = self.service_rx.poll_next_unpin(cx) {
			match event {
				ToServiceCommand::SetSyncForkRequest(peers, hash, number) => {
					self.chain_sync.set_sync_fork_request(peers, &hash, number);
				},
				ToServiceCommand::EventStream(tx) => self.event_streams.push(tx),
				ToServiceCommand::RequestJustification(hash, number) =>
					self.chain_sync.request_justification(&hash, number),
				ToServiceCommand::ClearJustificationRequests =>
					self.chain_sync.clear_justification_requests(),
				ToServiceCommand::BlocksProcessed(imported, count, results) => {
					for result in self.chain_sync.on_blocks_processed(imported, count, results) {
						match result {
							Ok((id, req)) => self.chain_sync.send_block_request(id, req),
							Err(BadPeer(id, repu)) => {
								self.network_service
									.disconnect_peer(id, self.block_announce_protocol_name.clone());
								self.network_service.report_peer(id, repu)
							},
						}