forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modular block request handler (paritytech#1524)
Submit the outstanding PRs from the old repos(these were already reviewed and approved before the repo rorg, but not yet submitted): Main PR: paritytech/substrate#14014 Companion PRs: paritytech/polkadot#7134, paritytech/cumulus#2489 The changes in the PR: 1. ChainSync currently calls into the block request handler directly. Instead, move the block request handler behind a trait. This allows new protocols to be plugged into ChainSync. 2. BuildNetworkParams is changed so that custom relay protocol implementations can be (optionally) passed in during network creation time. If custom protocol is not specified, it defaults to the existing block handler 3. BlockServer and BlockDownloader traits are introduced for the protocol implementation. The existing block handler has been changed to implement these traits 4. Other changes: [X] Make TxHash serializable. This is needed for exchanging the serialized hash in the relay protocol messages [X] Clean up types no longer used(OpaqueBlockRequest, OpaqueBlockResponse) --------- Co-authored-by: Dmitry Markin <dmitry@markin.tech> Co-authored-by: command-bot <>
- Loading branch information
1 parent
eb2a523
commit 85fe3e6
Showing
12 changed files
with
368 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Block relay protocol related definitions. | ||
|
||
use futures::channel::oneshot; | ||
use libp2p::PeerId; | ||
use sc_network::request_responses::{ProtocolConfig, RequestFailure}; | ||
use sc_network_common::sync::message::{BlockData, BlockRequest}; | ||
use sp_runtime::traits::Block as BlockT; | ||
use std::sync::Arc; | ||
|
||
/// The serving side of the block relay protocol. It runs a single instance | ||
/// of the server task that processes the incoming protocol messages. | ||
#[async_trait::async_trait] | ||
pub trait BlockServer<Block: BlockT>: Send { | ||
/// Starts the protocol processing. | ||
async fn run(&mut self); | ||
} | ||
|
||
/// The client side stub to download blocks from peers. This is a handle | ||
/// that can be used to initiate concurrent downloads. | ||
#[async_trait::async_trait] | ||
pub trait BlockDownloader<Block: BlockT>: Send + Sync { | ||
/// Performs the protocol specific sequence to fetch the blocks from the peer. | ||
/// Output: if the download succeeds, the response is a `Vec<u8>` which is | ||
/// in a format specific to the protocol implementation. The block data | ||
/// can be extracted from this response using [`BlockDownloader::block_response_into_blocks`]. | ||
async fn download_blocks( | ||
&self, | ||
who: PeerId, | ||
request: BlockRequest<Block>, | ||
) -> Result<Result<Vec<u8>, RequestFailure>, oneshot::Canceled>; | ||
|
||
/// Parses the protocol specific response to retrieve the block data. | ||
fn block_response_into_blocks( | ||
&self, | ||
request: &BlockRequest<Block>, | ||
response: Vec<u8>, | ||
) -> Result<Vec<BlockData<Block>>, BlockResponseError>; | ||
} | ||
|
||
/// Errors returned by [`BlockDownloader::block_response_into_blocks`]. | ||
#[derive(Debug)] | ||
pub enum BlockResponseError { | ||
/// Failed to decode the response bytes. | ||
DecodeFailed(String), | ||
|
||
/// Failed to extract the blocks from the decoded bytes. | ||
ExtractionFailed(String), | ||
} | ||
|
||
/// Block relay specific params for network creation, specified in | ||
/// ['sc_service::BuildNetworkParams']. | ||
pub struct BlockRelayParams<Block: BlockT> { | ||
pub server: Box<dyn BlockServer<Block>>, | ||
pub downloader: Arc<dyn BlockDownloader<Block>>, | ||
pub request_response_config: ProtocolConfig, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.