Modular block request handler (#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: https://github.com/paritytech/substrate/pull/14014
Companion PRs: https://github.com/paritytech/polkadot/pull/7134,
https://github.com/paritytech/cumulus/pull/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 <>
This commit is contained in:
Rahul Subramaniyam
2023-09-15 01:12:45 -07:00
committed by GitHub
parent c6df364157
commit b35b28ca4b
14 changed files with 370 additions and 256 deletions
+28 -18
View File
@@ -50,10 +50,10 @@ use sc_network_bitswap::BitswapRequestHandler;
use sc_network_common::role::Roles;
use sc_network_light::light_client_requests::handler::LightClientRequestHandler;
use sc_network_sync::{
block_request_handler::BlockRequestHandler, engine::SyncingEngine,
service::network::NetworkServiceProvider, state_request_handler::StateRequestHandler,
warp::WarpSyncParams, warp_request_handler::RequestHandler as WarpSyncRequestHandler,
SyncingService,
block_relay_protocol::BlockRelayParams, block_request_handler::BlockRequestHandler,
engine::SyncingEngine, service::network::NetworkServiceProvider,
state_request_handler::StateRequestHandler, warp::WarpSyncParams,
warp_request_handler::RequestHandler as WarpSyncRequestHandler, SyncingService,
};
use sc_rpc::{
author::AuthorApiServer,
@@ -696,6 +696,9 @@ pub struct BuildNetworkParams<'a, TBl: BlockT, TExPool, TImpQu, TCl> {
Option<Box<dyn FnOnce(Arc<TCl>) -> Box<dyn BlockAnnounceValidator<TBl> + Send> + Send>>,
/// Optional warp sync params.
pub warp_sync_params: Option<WarpSyncParams<TBl>>,
/// User specified block relay params. If not specified, the default
/// block request handler will be used.
pub block_relay: Option<BlockRelayParams<TBl>>,
}
/// Build the network service, the network status sinks and an RPC sender.
@@ -734,6 +737,7 @@ where
import_queue,
block_announce_validator_builder,
warp_sync_params,
block_relay,
} = params;
if warp_sync_params.is_none() && config.network.sync_mode.is_warp() {
@@ -757,19 +761,26 @@ where
Box::new(DefaultBlockAnnounceValidator)
};
let (block_request_protocol_config, block_request_protocol_name) = {
// Allow both outgoing and incoming requests.
let (handler, protocol_config) = BlockRequestHandler::new(
&protocol_id,
config.chain_spec.fork_id(),
client.clone(),
net_config.network_config.default_peers_set.in_peers as usize +
net_config.network_config.default_peers_set.out_peers as usize,
);
let config_name = protocol_config.name.clone();
spawn_handle.spawn("block-request-handler", Some("networking"), handler.run());
(protocol_config, config_name)
let (chain_sync_network_provider, chain_sync_network_handle) = NetworkServiceProvider::new();
let (mut block_server, block_downloader, block_request_protocol_config) = match block_relay {
Some(params) => (params.server, params.downloader, params.request_response_config),
None => {
// Custom protocol was not specified, use the default block handler.
// Allow both outgoing and incoming requests.
let params = BlockRequestHandler::new(
chain_sync_network_handle.clone(),
&protocol_id,
config.chain_spec.fork_id(),
client.clone(),
config.network.default_peers_set.in_peers as usize +
config.network.default_peers_set.out_peers as usize,
);
(params.server, params.downloader, params.request_response_config)
},
};
spawn_handle.spawn("block-request-handler", Some("networking"), async move {
block_server.run().await;
});
let (state_request_protocol_config, state_request_protocol_name) = {
let num_peer_hint = net_config.network_config.default_peers_set_num_full as usize +
@@ -860,7 +871,6 @@ where
spawn_handle.spawn("peer-store", Some("networking"), peer_store.run());
let (tx, rx) = sc_utils::mpsc::tracing_unbounded("mpsc_syncing_engine_protocol", 100_000);
let (chain_sync_network_provider, chain_sync_network_handle) = NetworkServiceProvider::new();
let (engine, sync_service, block_announce_config) = SyncingEngine::new(
Roles::from(&config.role),
client.clone(),
@@ -872,7 +882,7 @@ where
warp_sync_params,
chain_sync_network_handle,
import_queue.service(),
block_request_protocol_name,
block_downloader,
state_request_protocol_name,
warp_request_protocol_name,
rx,