diff --git a/client/network/src/light_client_requests.rs b/client/network/src/light_client_requests.rs index f859a35f45b24..148c95fde2a0d 100644 --- a/client/network/src/light_client_requests.rs +++ b/client/network/src/light_client_requests.rs @@ -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; @@ -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(); diff --git a/client/network/src/light_client_requests/handler.rs b/client/network/src/light_client_requests/handler.rs index 08de99a0a5de4..a91876c87d99f 100644 --- a/client/network/src/light_client_requests/handler.rs +++ b/client/network/src/light_client_requests/handler.rs @@ -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"; @@ -159,6 +152,7 @@ impl LightClientRequestHandler { request: &schema::v1::light::RemoteCallRequest, ) -> Result { log::trace!( + target: LOG_TARGET, "Remote call request from {} ({} at {:?}).", peer, request.method, request.block, ); @@ -172,10 +166,11 @@ impl LightClientRequestHandler { 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); } }; @@ -193,11 +188,12 @@ impl LightClientRequestHandler { request: &schema::v1::light::RemoteReadRequest, ) -> Result { 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, ); @@ -211,10 +207,11 @@ impl LightClientRequestHandler { 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); } }; @@ -232,11 +229,12 @@ impl LightClientRequestHandler { request: &schema::v1::light::RemoteReadChildRequest, ) -> Result { 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), @@ -259,6 +257,7 @@ impl LightClientRequestHandler { Ok(proof) => proof, Err(error) => { log::trace!( + target: LOG_TARGET, "remote read child request from {} ({} {} at {:?}) failed with: {}", peer, HexDisplay::from(&request.storage_key), @@ -266,7 +265,7 @@ impl LightClientRequestHandler { request.block, error, ); - StorageProof::empty() + return Err(HandleRequestError::NotFulfillable); } }; @@ -283,17 +282,18 @@ impl LightClientRequestHandler { peer: &PeerId, request: &schema::v1::light::RemoteHeaderRequest, ) -> Result { - 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); } }; @@ -311,6 +311,7 @@ impl LightClientRequestHandler { request: &schema::v1::light::RemoteChangesRequest, ) -> Result { log::trace!( + target: LOG_TARGET, "Remote changes proof request from {} for key {} ({:?}..{:?}).", peer, if !request.storage_key.is_empty() { @@ -337,6 +338,7 @@ impl LightClientRequestHandler { 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)), @@ -345,12 +347,7 @@ impl LightClientRequestHandler { error, ); - light::ChangesProof:: { - max_block: Zero::zero(), - proof: Vec::new(), - roots: BTreeMap::new(), - roots_proof: StorageProof::empty(), - } + return Err(HandleRequestError::NotFulfillable); } }; @@ -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),