Skip to content
Merged
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
36 changes: 17 additions & 19 deletions crates/sc-subspace-block-relay/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ enum ServerMessage<Block: BlockT, ProtocolRequest> {
ProtocolRequest(ProtocolRequest),
}

impl<Block: BlockT, ProtocolRequest> From<InitialRequest<Block, ProtocolRequest>>
for ServerMessage<Block, ProtocolRequest>
{
fn from(
inner: InitialRequest<Block, ProtocolRequest>,
) -> ServerMessage<Block, ProtocolRequest> {
ServerMessage::InitialRequest(inner)
}
}

impl<Block: BlockT, ProtocolRequest> From<ProtocolRequest>
for ServerMessage<Block, ProtocolRequest>
{
Expand Down Expand Up @@ -135,9 +125,9 @@ where
request: BlockRequest<Block>,
) -> Result<DownloadResult<BlockHash<Block>, BlockData<Block>>, RelayError> {
let start_ts = Instant::now();
let network_peer_handle: NetworkPeerHandle<ServerMessage<Block, ProtoClient::Request>> =
self.network
.network_peer_handle(self.protocol_name.clone(), who)?;
let network_peer_handle = self
.network
.network_peer_handle(self.protocol_name.clone(), who)?;

// Perform the initial request/response
let initial_request = InitialRequest {
Expand All @@ -149,14 +139,19 @@ where
protocol_request: self.protocol_client.build_initial_request(),
};
let initial_response = network_peer_handle
.request::<_, InitialResponse<Block, ProtoClient::Response>>(initial_request)
.request::<_, InitialResponse<Block, ProtoClient::Response>>(
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(protocol_response, &network_peer_handle)
.resolve_extrinsics::<ServerMessage<Block, ProtoClient::Request>>(
protocol_response,
&network_peer_handle,
)
.await?;
(Some(body), local_miss)
} else {
Expand Down Expand Up @@ -184,14 +179,17 @@ where
}

/// Resolves the extrinsics from the initial response
async fn resolve_extrinsics(
async fn resolve_extrinsics<Request>(
&self,
protocol_response: ProtoClient::Response,
network_peer_handle: &NetworkPeerHandle<ServerMessage<Block, ProtoClient::Request>>,
) -> Result<(Vec<Extrinsic<Block>>, usize), RelayError> {
network_peer_handle: &NetworkPeerHandle,
) -> Result<(Vec<Extrinsic<Block>>, usize), RelayError>
where
Request: From<ProtoClient::Request> + Encode + Send + Sync,
{
let (block_hash, resolved) = self
.protocol_client
.resolve_initial_response(protocol_response, network_peer_handle)
.resolve_initial_response::<Request>(protocol_response, network_peer_handle)
.await?;
let mut local_miss = 0;
let extrinsics = resolved
Expand Down
8 changes: 5 additions & 3 deletions crates/sc-subspace-block-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ where
fn build_initial_request(&self) -> Self::Request;

/// Resolves the initial response to produce the protocol units.
async fn resolve_initial_response<RequestMsg: From<Self::Request> + Encode + Send + Sync>(
async fn resolve_initial_response<Request>(
&self,
response: Self::Response,
network_peer_handle: &NetworkPeerHandle<RequestMsg>,
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError>;
network_peer_handle: &NetworkPeerHandle,
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError>
where
Request: From<Self::Request> + Encode + Send + Sync;
}

/// The server side of the relay protocol
Expand Down
20 changes: 11 additions & 9 deletions crates/sc-subspace-block-relay/src/protocol/compact_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,14 @@ where
}

/// Fetches the missing entries from the server
async fn resolve_misses<RequestMsg>(
async fn resolve_misses<Request>(
&self,
compact_response: InitialResponse<DownloadUnitId, ProtocolUnitId, ProtocolUnit>,
context: ResolveContext<ProtocolUnitId, ProtocolUnit>,
network_peer_handle: &NetworkPeerHandle<RequestMsg>,
network_peer_handle: &NetworkPeerHandle,
) -> Result<Vec<Resolved<ProtocolUnitId, ProtocolUnit>>, RelayError>
where
RequestMsg:
From<CompactBlockRequest<DownloadUnitId, ProtocolUnitId>> + Encode + Send + Sync,
Request: From<CompactBlockRequest<DownloadUnitId, ProtocolUnitId>> + Encode + Send + Sync,
{
let ResolveContext {
mut resolved,
Expand All @@ -167,7 +166,7 @@ where
protocol_unit_ids: local_miss.clone(),
});
let response: CompactBlockResponse<DownloadUnitId, ProtocolUnitId, ProtocolUnit> =
network_peer_handle.request(request).await?;
network_peer_handle.request(Request::from(request)).await?;
let missing_entries_response =
if let CompactBlockResponse::MissingEntries(response) = response {
response
Expand Down Expand Up @@ -218,11 +217,14 @@ where
CompactBlockRequest::Initial
}

async fn resolve_initial_response<RequestMsg: From<Self::Request> + Encode + Send + Sync>(
async fn resolve_initial_response<Request>(
&self,
response: Self::Response,
network_peer_handle: &NetworkPeerHandle<RequestMsg>,
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError> {
network_peer_handle: &NetworkPeerHandle,
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError>
where
Request: From<Self::Request> + Encode + Send + Sync,
{
let compact_response = match response {
CompactBlockResponse::Initial(compact_response) => compact_response,
_ => return Err(RelayError::UnexpectedInitialResponse),
Expand All @@ -247,7 +249,7 @@ where
let misses = context.local_miss.len();
let download_unit_id = compact_response.download_unit_id.clone();
let resolved = self
.resolve_misses(compact_response, context, network_peer_handle)
.resolve_misses::<Request>(compact_response, context, network_peer_handle)
.await?;
trace!(
target: LOG_TARGET,
Expand Down
21 changes: 9 additions & 12 deletions crates/sc-subspace-block-relay/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Common utils.

use codec::{self, Decode, Encode};
use codec::{Decode, Encode};
use futures::channel::oneshot;
use parking_lot::Mutex;
use sc_network::request_responses::IfDisconnected;
Expand Down Expand Up @@ -31,11 +31,11 @@ impl NetworkWrapper {
*self.network.lock() = Some(network);
}

pub(crate) fn network_peer_handle<RequestMsg: Encode>(
pub(crate) fn network_peer_handle(
&self,
protocol_name: ProtocolName,
who: PeerId,
) -> Result<NetworkPeerHandle<RequestMsg>, RequestResponseErr> {
) -> Result<NetworkPeerHandle, RequestResponseErr> {
match self.network.lock().as_ref().cloned() {
Some(network) => Ok(NetworkPeerHandle::new(protocol_name, who, network)),
None => Err(RequestResponseErr::NetworkUninitialized),
Expand All @@ -44,40 +44,37 @@ impl NetworkWrapper {
}

/// Network handle that allows making requests to specific peer and protocol.
/// `RequestMsg` is the format of the request message sent on the wire.
/// `Request` is the format of the request message sent on the wire.
#[derive(Clone)]
pub(crate) struct NetworkPeerHandle<RequestMsg: Encode> {
pub(crate) struct NetworkPeerHandle {
protocol_name: ProtocolName,
who: PeerId,
network: NetworkRequestService,
_p: std::marker::PhantomData<RequestMsg>,
}

impl<RequestMsg: Encode> NetworkPeerHandle<RequestMsg> {
impl NetworkPeerHandle {
fn new(protocol_name: ProtocolName, who: PeerId, network: NetworkRequestService) -> Self {
Self {
protocol_name,
who,
network,
_p: Default::default(),
}
}

/// Performs the request, where the request can be transformed into
/// the network format `RequestMsg`.
/// Performs the request
pub(crate) async fn request<Request, Response>(
&self,
request: Request,
) -> Result<Response, RequestResponseErr>
where
Request: Into<RequestMsg>,
Request: Encode,
Response: Decode,
{
let (tx, rx) = oneshot::channel();
self.network.start_request(
self.who,
self.protocol_name.clone(),
Request::into(request).encode(),
request.encode(),
tx,
IfDisconnected::ImmediateError,
);
Expand Down