Repository: paritytech/substrate
File: client/beefy/src/communication/request_response/incoming_requests_handler.rs (Rust)

Code before:
{
	/// Create a new [`BeefyJustifsRequestHandler`].
	pub fn new<Hash: AsRef<[u8]>>(
		genesis_hash: Hash,
		fork_id: Option<&str>,
		client: Arc<Client>,
	) -> (Self, RequestResponseConfig) {
		let (request_receiver, config) =
			on_demand_justifications_protocol_config(genesis_hash, fork_id);
		let justif_protocol_name = config.name.clone();

		(Self { request_receiver, justif_protocol_name, client, _block: PhantomData }, config)
	}

	/// Network request-response protocol name used by this handler.
	pub fn protocol_name(&self) -> ProtocolName {
		self.justif_protocol_name.clone()
	}

	// Sends back justification response if justification found in client backend.
	fn handle_request(&self, request: IncomingRequest<B>) -> Result<(), Error> {
		// TODO (issue #12293): validate `request` and change peer reputation for invalid requests.

		let hash = match self.client.block_hash(request.payload.begin).map_err(Error::Client)? {
			Some(hash) => hash,
			None => return Ok(()),
		};

		let maybe_encoded_proof = self
			.client
			.justifications(&hash)
			.map_err(Error::Client)?
			.and_then(|justifs| justifs.get(BEEFY_ENGINE_ID).cloned())
			// No BEEFY justification present.
			.ok_or(());

		request
			.pending_response
			.send(netconfig::OutgoingResponse {
				result: maybe_encoded_proof,
				reputation_changes: Vec::new(),
				sent_feedback: None,
			})
			.map_err(|_| Error::SendResponse)
	}

	/// Run [`BeefyJustifsRequestHandler`].
	pub async fn run(mut self) {
		trace!(target: "beefy::sync", "🥩 Running BeefyJustifsRequestHandler");

		while let Ok(request) = self.request_receiver.recv(|| vec![]).await {

Review comment (suggestion):
If the block hash can't be found here, handler should also send an `Err(())` response to this request.

Aka, just chain this block together with next block so that `maybe_encoded_proof = Err(())` will be sent when there is no hash availalable to the client.

Code after:
{
	/// Create a new [`BeefyJustifsRequestHandler`].
	pub fn new<Hash: AsRef<[u8]>>(
		genesis_hash: Hash,
		fork_id: Option<&str>,
		client: Arc<Client>,
	) -> (Self, RequestResponseConfig) {
		let (request_receiver, config) =
			on_demand_justifications_protocol_config(genesis_hash, fork_id);
		let justif_protocol_name = config.name.clone();

		(Self { request_receiver, justif_protocol_name, client, _block: PhantomData }, config)
	}

	/// Network request-response protocol name used by this handler.
	pub fn protocol_name(&self) -> ProtocolName {
		self.justif_protocol_name.clone()
	}

	// Sends back justification response if justification found in client backend.
	fn handle_request(&self, request: IncomingRequest<B>) -> Result<(), Error> {
		// TODO (issue #12293): validate `request` and change peer reputation for invalid requests.

		let maybe_encoded_proof = if let Some(hash) =
			self.client.block_hash(request.payload.begin).map_err(Error::Client)?
		{
			self.client
				.justifications(&hash)
				.map_err(Error::Client)?
				.and_then(|justifs| justifs.get(BEEFY_ENGINE_ID).cloned())
				// No BEEFY justification present.
				.ok_or(())
		} else {
			Err(())
		};

		request
			.pending_response
			.send(netconfig::OutgoingResponse {
				result: maybe_encoded_proof,
				reputation_changes: Vec::new(),
				sent_feedback: None,
			})
			.map_err(|_| Error::SendResponse)
	}

	/// Run [`BeefyJustifsRequestHandler`].
	pub async fn run(mut self) {
		trace!(target: "beefy::sync", "🥩 Running BeefyJustifsRequestHandler");

		while let Ok(request) = self.request_receiver.recv(|| vec![]).await {