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
22 changes: 18 additions & 4 deletions crates/sc-subspace-block-relay/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ enum ServerMessage<Block: BlockT, ProtocolRequest> {
ProtocolRequest(ProtocolRequest),
}

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

/// The client side of the consensus block relay
struct ConsensusRelayClient<Block, Pool, ProtoClient>
where
Expand Down Expand Up @@ -140,7 +148,10 @@ where
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 @@ -168,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,
) -> Result<(Vec<Extrinsic<Block>>, usize), RelayError> {
) -> 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: 4 additions & 4 deletions crates/sc-subspace-block-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ where
fn build_initial_request(&self) -> Self::Request;

/// Resolves the initial response to produce the protocol units.
/// `encoder_fn` needs to be called to generate the request payload
/// as part of request/response sequences initiated by the protocol.
async fn resolve_initial_response(
async fn resolve_initial_response<Request>(
&self,
response: Self::Response,
network_peer_handle: &NetworkPeerHandle,
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError>;
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError>
where
Request: From<Self::Request> + Encode + Send + Sync;
}

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

/// Fetches the missing entries from the server
async fn resolve_misses(
async fn resolve_misses<Request>(
&self,
compact_response: InitialResponse<DownloadUnitId, ProtocolUnitId, ProtocolUnit>,
context: ResolveContext<ProtocolUnitId, ProtocolUnit>,
network_peer_handle: &NetworkPeerHandle,
) -> Result<Vec<Resolved<ProtocolUnitId, ProtocolUnit>>, RelayError> {
) -> Result<Vec<Resolved<ProtocolUnitId, ProtocolUnit>>, RelayError>
where
Request: From<CompactBlockRequest<DownloadUnitId, ProtocolUnitId>> + Encode + Send + Sync,
{
let ResolveContext {
mut resolved,
local_miss,
Expand All @@ -163,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 @@ -214,11 +217,14 @@ where
CompactBlockRequest::Initial
}

async fn resolve_initial_response(
async fn resolve_initial_response<Request>(
&self,
response: Self::Response,
network_peer_handle: &NetworkPeerHandle,
) -> Result<(DownloadUnitId, Vec<Resolved<ProtocolUnitId, ProtocolUnit>>), RelayError> {
) -> 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 @@ -243,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
3 changes: 2 additions & 1 deletion 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 @@ -44,6 +44,7 @@ impl NetworkWrapper {
}

/// Network handle that allows making requests to specific peer and protocol.
/// `Request` is the format of the request message sent on the wire.
#[derive(Clone)]
pub(crate) struct NetworkPeerHandle {
protocol_name: ProtocolName,
Expand Down