Move block/state/warpc sync requests/responses to ChainSync (#12739)

* Move block/state/warpc sync requests/responses to `ChainSync`

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply review suggestions

* cargo-fmt + doc fix

* Fix tests

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Aaro Altonen
2022-11-22 10:19:17 +02:00
committed by GitHub
parent 4cb24da8f2
commit 1b5d52deb2
16 changed files with 1094 additions and 1126 deletions
@@ -27,8 +27,8 @@ use sc_block_builder::BlockBuilderProvider;
use sc_client_api::HeaderBackend;
use sc_consensus::JustificationSyncLink;
use sc_network_common::{
config::{MultiaddrWithPeerId, SetConfig},
protocol::event::Event,
config::{MultiaddrWithPeerId, ProtocolId, SetConfig},
protocol::{event::Event, role::Roles, ProtocolName},
service::NetworkSyncForkRequest,
sync::{SyncState, SyncStatus},
};
@@ -39,7 +39,6 @@ use sp_runtime::{
traits::{Block as BlockT, Header as _},
};
use std::{
iter,
sync::{Arc, RwLock},
task::Poll,
time::Duration,
@@ -49,10 +48,6 @@ use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt as _
fn set_default_expecations_no_peers(
chain_sync: &mut MockChainSync<substrate_test_runtime_client::runtime::Block>,
) {
chain_sync.expect_block_requests().returning(|| Box::new(iter::empty()));
chain_sync.expect_state_request().returning(|| None);
chain_sync.expect_justification_requests().returning(|| Box::new(iter::empty()));
chain_sync.expect_warp_sync_request().returning(|| None);
chain_sync.expect_poll().returning(|_| Poll::Pending);
chain_sync.expect_status().returning(|| SyncStatus {
state: SyncState::Idle,
@@ -342,13 +337,19 @@ async fn disconnect_peer_using_chain_sync_handle() {
sc_network_sync::service::network::NetworkServiceProvider::new();
let handle_clone = chain_sync_network_handle.clone();
let (chain_sync, chain_sync_service) = ChainSync::new(
let (chain_sync, chain_sync_service, _) = ChainSync::new(
sc_network_common::sync::SyncMode::Full,
client.clone(),
ProtocolId::from("test-protocol-name"),
&Some(String::from("test-fork-id")),
Roles::from(&config::Role::Full),
Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator),
1u32,
None,
chain_sync_network_handle.clone(),
ProtocolName::from("block-request"),
ProtocolName::from("state-request"),
None,
)
.unwrap();
@@ -216,31 +216,6 @@ impl TestNetworkBuilder {
None,
)));
let (chain_sync_network_provider, chain_sync_network_handle) =
self.chain_sync_network.unwrap_or(NetworkServiceProvider::new());
let (chain_sync, chain_sync_service) = self.chain_sync.unwrap_or({
let (chain_sync, chain_sync_service) = ChainSync::new(
match network_config.sync_mode {
config::SyncMode::Full => sc_network_common::sync::SyncMode::Full,
config::SyncMode::Fast { skip_proofs, storage_chain_mode } =>
sc_network_common::sync::SyncMode::LightState {
skip_proofs,
storage_chain_mode,
},
config::SyncMode::Warp => sc_network_common::sync::SyncMode::Warp,
},
client.clone(),
Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator),
network_config.max_parallel_downloads,
None,
chain_sync_network_handle,
)
.unwrap();
(Box::new(chain_sync), chain_sync_service)
});
let protocol_id = ProtocolId::from("test-protocol-name");
let fork_id = Some(String::from("test-fork-id"));
@@ -289,6 +264,37 @@ impl TestNetworkBuilder {
},
};
let (chain_sync_network_provider, chain_sync_network_handle) =
self.chain_sync_network.unwrap_or(NetworkServiceProvider::new());
let (chain_sync, chain_sync_service) = self.chain_sync.unwrap_or({
let (chain_sync, chain_sync_service, _) = ChainSync::new(
match network_config.sync_mode {
config::SyncMode::Full => sc_network_common::sync::SyncMode::Full,
config::SyncMode::Fast { skip_proofs, storage_chain_mode } =>
sc_network_common::sync::SyncMode::LightState {
skip_proofs,
storage_chain_mode,
},
config::SyncMode::Warp => sc_network_common::sync::SyncMode::Warp,
},
client.clone(),
protocol_id.clone(),
&fork_id,
Roles::from(&config::Role::Full),
Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator),
network_config.max_parallel_downloads,
None,
chain_sync_network_handle,
block_request_protocol_config.name.clone(),
state_request_protocol_config.name.clone(),
None,
)
.unwrap();
(Box::new(chain_sync), chain_sync_service)
});
let worker = NetworkWorker::<
substrate_test_runtime_client::runtime::Block,
substrate_test_runtime_client::runtime::Hash,
@@ -305,11 +311,12 @@ impl TestNetworkBuilder {
chain_sync,
chain_sync_service,
metrics_registry: None,
block_request_protocol_config,
state_request_protocol_config,
light_client_request_protocol_config,
warp_sync_protocol_config: None,
request_response_protocol_configs: Vec::new(),
request_response_protocol_configs: [
block_request_protocol_config,
state_request_protocol_config,
light_client_request_protocol_config,
]
.to_vec(),
})
.unwrap();