From 602170f6d4213c1ad1aca258df81e14de31c6ebb Mon Sep 17 00:00:00 2001 From: Rahul Subramaniyam Date: Mon, 28 Aug 2023 18:46:17 -0700 Subject: [PATCH] Address comments --- .../sc-subspace-block-relay/src/consensus.rs | 117 +++++++++--------- 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/crates/sc-subspace-block-relay/src/consensus.rs b/crates/sc-subspace-block-relay/src/consensus.rs index 5e0c241f3e7..70d6cdbf6b3 100644 --- a/crates/sc-subspace-block-relay/src/consensus.rs +++ b/crates/sc-subspace-block-relay/src/consensus.rs @@ -27,6 +27,7 @@ use sp_runtime::Justifications; use std::num::{NonZeroU32, NonZeroUsize}; use std::sync::Arc; use std::time::{Duration, Instant}; +use tracing::{debug, info, trace, warn}; type BlockHash = ::Hash; type BlockHeader = ::Header; @@ -116,24 +117,25 @@ impl PartialBlock { } /// The message to the server +#[allow(clippy::enum_variant_names)] #[derive(Encode, Decode)] -enum ServerRequest { +enum ServerMessage { /// Initial message, to be handled both by the client /// and the protocol - Initial(InitialRequest), + InitialRequest(InitialRequest), /// Message to be handled by the protocol - Protocol(ProtocolRequest), + ProtocolRequest(ProtocolRequest), /// Full download request. - FullDownload(FullDownloadRequest), + FullDownloadRequest(FullDownloadRequest), } impl From - for ServerRequest + for ServerMessage { - fn from(inner: ProtocolRequest) -> ServerRequest { - ServerRequest::Protocol(inner) + fn from(inner: ProtocolRequest) -> ServerMessage { + ServerMessage::ProtocolRequest(inner) } } @@ -161,7 +163,7 @@ where &self, who: PeerId, request: BlockRequest, - ) -> Result, RelayError> { + ) -> Result>, RelayError> { let start_ts = Instant::now(); let network_peer_handle = self .network @@ -177,16 +179,16 @@ where protocol_request: self.protocol_client.build_initial_request(), }; let initial_response = network_peer_handle - .request::<_, InitialResponse>(ServerRequest::Initial( - initial_request, - )) + .request::<_, InitialResponse>( + ServerMessage::InitialRequest(initial_request), + ) .await?; // Resolve the protocol response to get the extrinsics let (body, local_miss) = if let Some(protocol_response) = initial_response.protocol_response { let (body, local_miss) = self - .resolve_extrinsics::>( + .resolve_extrinsics::>( protocol_response, &network_peer_handle, ) @@ -197,11 +199,11 @@ where }; // Assemble the final response - let downloaded = vec![initial_response.partial_block.block_data(body)].encode(); - tracing::debug!( + let downloaded = vec![initial_response.partial_block.block_data(body)]; + debug!( target: LOG_TARGET, block_hash = ?initial_response.block_hash, - download_len = %downloaded.len(), + download_bytes = %downloaded.encoded_size(), %local_miss, duration = ?start_ts.elapsed(), "block_download", @@ -209,6 +211,35 @@ where Ok(downloaded) } + /// Downloads the requested blocks from the peer, without using the relay protocol. + async fn full_download( + &self, + who: PeerId, + request: BlockRequest, + ) -> Result>, RelayError> { + let start_ts = Instant::now(); + let network_peer_handle = self + .network + .network_peer_handle(self.protocol_name.clone(), who)?; + + let server_request: ServerMessage = + ServerMessage::FullDownloadRequest(FullDownloadRequest(request.clone())); + let full_response = network_peer_handle + .request::<_, FullDownloadResponse>(server_request) + .await?; + let downloaded = full_response.0; + + debug!( + target: LOG_TARGET, + ?request, + download_blocks = %downloaded.len(), + download_bytes = %downloaded.encoded_size(), + duration = ?start_ts.elapsed(), + "full_download", + ); + Ok(downloaded) + } + /// Resolves the extrinsics from the initial response async fn resolve_extrinsics( &self, @@ -228,7 +259,7 @@ where .map(|entry| { let encoded = entry.protocol_unit.encode(); if !entry.locally_resolved { - tracing::trace!( + trace!( target: LOG_TARGET, ?block_hash, tx_hash = ?entry.protocol_unit_id, @@ -242,36 +273,6 @@ where .collect(); Ok((extrinsics, local_miss)) } - - /// Downloads the requested blocks from the peer, without using the relay protocol. - async fn full_download( - &self, - who: PeerId, - request: BlockRequest, - ) -> Result, RelayError> { - let start_ts = Instant::now(); - let network_peer_handle = self - .network - .network_peer_handle(self.protocol_name.clone(), who)?; - - let server_request: ServerRequest = - ServerRequest::FullDownload(FullDownloadRequest(request.clone())); - let full_response = network_peer_handle - .request::<_, FullDownloadResponse>(server_request) - .await?; - let downloaded_blocks = full_response.0.len(); - let downloaded = full_response.0.encode(); - - tracing::debug!( - target: LOG_TARGET, - ?request, - %downloaded_blocks, - download_len = %downloaded.len(), - duration = ?start_ts.elapsed(), - "full_download", - ); - Ok(downloaded) - } } #[async_trait] @@ -294,9 +295,9 @@ where self.download(who, request.clone()).await }; match ret { - Ok(downloaded) => Ok(Ok(downloaded)), + Ok(blocks) => Ok(Ok(blocks.encode())), Err(error) => { - tracing::debug!( + debug!( target: LOG_TARGET, peer=?who, ?request, @@ -364,11 +365,11 @@ where payload, pending_response, } = request; - let server_msg: ServerRequest = + let server_msg: ServerMessage = match Decode::decode(&mut payload.as_ref()) { Ok(msg) => msg, Err(err) => { - tracing::warn!( + warn!( target: LOG_TARGET, ?peer, ?err, @@ -379,22 +380,22 @@ where }; let ret = match server_msg { - ServerRequest::Initial(req) => self.on_initial_request(req), - ServerRequest::Protocol(req) => self.on_protocol_request(req), - ServerRequest::FullDownload(req) => self.on_full_download_request(req), + ServerMessage::InitialRequest(req) => self.on_initial_request(req), + ServerMessage::ProtocolRequest(req) => self.on_protocol_request(req), + ServerMessage::FullDownloadRequest(req) => self.on_full_download_request(req), }; match ret { Ok(response) => { self.send_response(peer, response, pending_response); - tracing::trace!( + trace!( target: LOG_TARGET, ?peer, "server: request processed from" ); } Err(error) => { - tracing::debug!( + debug!( target: LOG_TARGET, ?peer, ?error, @@ -566,7 +567,7 @@ where sent_feedback: None, }; if sender.send(response).is_err() { - tracing::warn!( + warn!( target: LOG_TARGET, ?peer, "Failed to send response" @@ -584,7 +585,7 @@ where ProtoServer: ProtocolServer> + Send, { async fn run(&mut self) { - tracing::info!( + info!( target: LOG_TARGET, "relay::consensus block server: starting" ); @@ -681,7 +682,7 @@ where return Ok(Some(extrinsic)); } } - tracing::trace!( + trace!( target: LOG_TARGET, ?tx_hash, ?block_hash,