Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fix light requests handler error handling and logging target #8046

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/network/src/light_client_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
#[cfg(test)]
mod tests {
use super::*;
use crate::request_responses::IncomingRequest;
use crate::request_responses::{IncomingRequest, RequestFailure};
use crate::config::ProtocolId;

use assert_matches::assert_matches;
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
pending_response: tx,
})).unwrap();
pool.spawner().spawn_obj(async move {
pending_response.send(Ok(rx.await.unwrap().result.unwrap())).unwrap();
pending_response.send(rx.await.unwrap().result.map_err(|()| RequestFailure::Refused)).unwrap();
}.boxed().into()).unwrap();

pool.spawner().spawn_obj(sender.for_each(|_| future::ready(())).boxed().into()).unwrap();
Expand Down
44 changes: 22 additions & 22 deletions client/network/src/light_client_requests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,16 @@ use crate::{
use crate::request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig};
use futures::{channel::mpsc, prelude::*};
use prost::Message;
use sc_client_api::{
StorageProof,
light
};
use sc_peerset::ReputationChange;
use sp_core::{
storage::{ChildInfo, ChildType,StorageKey, PrefixedStorageKey},
hexdisplay::HexDisplay,
};
use sp_runtime::{
traits::{Block, Zero},
traits::Block,
generic::BlockId,
};
use std::{
collections::{BTreeMap},
sync::Arc,
};
use std::sync::Arc;
use log::debug;

const LOG_TARGET: &str = "light-client-request-handler";
Expand Down Expand Up @@ -159,6 +152,7 @@ impl<B: Block> LightClientRequestHandler<B> {
request: &schema::v1::light::RemoteCallRequest,
) -> Result<schema::v1::light::Response, HandleRequestError> {
log::trace!(
target: LOG_TARGET,
"Remote call request from {} ({} at {:?}).",
peer, request.method, request.block,
);
Expand All @@ -172,10 +166,11 @@ impl<B: Block> LightClientRequestHandler<B> {
Ok((_, proof)) => proof,
Err(e) => {
log::trace!(
target: LOG_TARGET,
"remote call request from {} ({} at {:?}) failed with: {}",
peer, request.method, request.block, e,
);
StorageProof::empty()
return Err(HandleRequestError::NotFulfillable);
}
};

Expand All @@ -193,11 +188,12 @@ impl<B: Block> LightClientRequestHandler<B> {
request: &schema::v1::light::RemoteReadRequest,
) -> Result<schema::v1::light::Response, HandleRequestError> {
if request.keys.is_empty() {
log::debug!("Invalid remote read request sent by {}.", peer);
log::debug!(target: LOG_TARGET, "Invalid remote read request sent by {}.", peer);
return Err(HandleRequestError::BadRequest("Remote read request without keys."))
}

log::trace!(
target: LOG_TARGET,
"Remote read request from {} ({} at {:?}).",
peer, fmt_keys(request.keys.first(), request.keys.last()), request.block,
);
Expand All @@ -211,10 +207,11 @@ impl<B: Block> LightClientRequestHandler<B> {
Ok(proof) => proof,
Err(error) => {
log::trace!(
target: LOG_TARGET,
"remote read request from {} ({} at {:?}) failed with: {}",
peer, fmt_keys(request.keys.first(), request.keys.last()), request.block, error,
);
StorageProof::empty()
return Err(HandleRequestError::NotFulfillable);
}
};

Expand All @@ -232,11 +229,12 @@ impl<B: Block> LightClientRequestHandler<B> {
request: &schema::v1::light::RemoteReadChildRequest,
) -> Result<schema::v1::light::Response, HandleRequestError> {
if request.keys.is_empty() {
log::debug!("Invalid remote child read request sent by {}.", peer);
log::debug!(target: LOG_TARGET, "Invalid remote child read request sent by {}.", peer);
return Err(HandleRequestError::BadRequest("Remove read child request without keys."))
}

log::trace!(
target: LOG_TARGET,
"Remote read child request from {} ({} {} at {:?}).",
peer,
HexDisplay::from(&request.storage_key),
Expand All @@ -259,14 +257,15 @@ impl<B: Block> LightClientRequestHandler<B> {
Ok(proof) => proof,
Err(error) => {
log::trace!(
target: LOG_TARGET,
"remote read child request from {} ({} {} at {:?}) failed with: {}",
peer,
HexDisplay::from(&request.storage_key),
fmt_keys(request.keys.first(), request.keys.last()),
request.block,
error,
);
StorageProof::empty()
return Err(HandleRequestError::NotFulfillable);
}
};

Expand All @@ -283,17 +282,18 @@ impl<B: Block> LightClientRequestHandler<B> {
peer: &PeerId,
request: &schema::v1::light::RemoteHeaderRequest,
) -> Result<schema::v1::light::Response, HandleRequestError> {
log::trace!("Remote header proof request from {} ({:?}).", peer, request.block);
log::trace!(target: LOG_TARGET, "Remote header proof request from {} ({:?}).", peer, request.block);

let block = Decode::decode(&mut request.block.as_ref())?;
let (header, proof) = match self.client.header_proof(&BlockId::Number(block)) {
Ok((header, proof)) => (header.encode(), proof),
Err(error) => {
log::trace!(
target: LOG_TARGET,
"Remote header proof request from {} ({:?}) failed with: {}.",
peer, request.block, error
);
(Default::default(), StorageProof::empty())
return Err(HandleRequestError::NotFulfillable);
}
};

Expand All @@ -311,6 +311,7 @@ impl<B: Block> LightClientRequestHandler<B> {
request: &schema::v1::light::RemoteChangesRequest,
) -> Result<schema::v1::light::Response, HandleRequestError> {
log::trace!(
target: LOG_TARGET,
"Remote changes proof request from {} for key {} ({:?}..{:?}).",
peer,
if !request.storage_key.is_empty() {
Expand All @@ -337,6 +338,7 @@ impl<B: Block> LightClientRequestHandler<B> {
Ok(proof) => proof,
Err(error) => {
log::trace!(
target: LOG_TARGET,
"Remote changes proof request from {} for key {} ({:?}..{:?}) failed with: {}.",
peer,
format!("{} : {}", HexDisplay::from(&request.storage_key), HexDisplay::from(&key.0)),
Expand All @@ -345,12 +347,7 @@ impl<B: Block> LightClientRequestHandler<B> {
error,
);

light::ChangesProof::<B::Header> {
max_block: Zero::zero(),
proof: Vec::new(),
roots: BTreeMap::new(),
roots_proof: StorageProof::empty(),
}
return Err(HandleRequestError::NotFulfillable);
}
};

Expand Down Expand Up @@ -381,6 +378,9 @@ enum HandleRequestError {
/// A bad request has been received.
#[display(fmt = "bad request: {}", _0)]
BadRequest(&'static str),
/// Received request concerns blocks that aren't available, or a similar client-side problem.
#[display(fmt = "Request cannot be fulfilled")]
NotFulfillable,
/// Encoding or decoding of some data failed.
#[display(fmt = "codec error: {}", _0)]
Codec(codec::Error),
Expand Down