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
File diff suppressed because it is too large Load Diff
+9 -22
View File
@@ -24,10 +24,8 @@ use libp2p::PeerId;
use sc_consensus::{BlockImportError, BlockImportStatus};
use sc_network_common::sync::{
message::{BlockAnnounce, BlockData, BlockRequest, BlockResponse},
warp::{EncodedProof, WarpProofRequest},
BadPeer, ChainSync as ChainSyncT, Metrics, OnBlockData, OnBlockJustification, OnStateData,
OpaqueBlockRequest, OpaqueBlockResponse, OpaqueStateRequest, OpaqueStateResponse, PeerInfo,
PollBlockAnnounceValidation, SyncStatus,
BadPeer, ChainSync as ChainSyncT, Metrics, OnBlockData, OnBlockJustification,
OpaqueBlockResponse, PeerInfo, PollBlockAnnounceValidation, PollResult, SyncStatus,
};
use sp_runtime::traits::{Block as BlockT, NumberFor};
@@ -40,6 +38,7 @@ mockall::mock! {
fn num_sync_requests(&self) -> usize;
fn num_downloaded_blocks(&self) -> usize;
fn num_peers(&self) -> usize;
fn num_active_peers(&self) -> usize;
fn new_peer(
&mut self,
who: PeerId,
@@ -55,24 +54,12 @@ mockall::mock! {
hash: &Block::Hash,
number: NumberFor<Block>,
);
fn justification_requests<'a>(
&'a mut self,
) -> Box<dyn Iterator<Item = (PeerId, BlockRequest<Block>)> + 'a>;
fn block_requests<'a>(&'a mut self) -> Box<dyn Iterator<Item = (PeerId, BlockRequest<Block>)> + 'a>;
fn state_request(&mut self) -> Option<(PeerId, OpaqueStateRequest)>;
fn warp_sync_request(&mut self) -> Option<(PeerId, WarpProofRequest<Block>)>;
fn on_block_data(
&mut self,
who: &PeerId,
request: Option<BlockRequest<Block>>,
response: BlockResponse<Block>,
) -> Result<OnBlockData<Block>, BadPeer>;
fn on_state_data(
&mut self,
who: &PeerId,
response: OpaqueStateResponse,
) -> Result<OnStateData<Block>, BadPeer>;
fn on_warp_sync_data(&mut self, who: &PeerId, response: EncodedProof) -> Result<(), BadPeer>;
fn on_block_justification(
&mut self,
who: PeerId,
@@ -104,19 +91,19 @@ mockall::mock! {
) -> Poll<PollBlockAnnounceValidation<Block::Header>>;
fn peer_disconnected(&mut self, who: &PeerId) -> Option<OnBlockData<Block>>;
fn metrics(&self) -> Metrics;
fn create_opaque_block_request(&self, request: &BlockRequest<Block>) -> OpaqueBlockRequest;
fn encode_block_request(&self, request: &OpaqueBlockRequest) -> Result<Vec<u8>, String>;
fn decode_block_response(&self, response: &[u8]) -> Result<OpaqueBlockResponse, String>;
fn block_response_into_blocks(
&self,
request: &BlockRequest<Block>,
response: OpaqueBlockResponse,
) -> Result<Vec<BlockData<Block>>, String>;
fn encode_state_request(&self, request: &OpaqueStateRequest) -> Result<Vec<u8>, String>;
fn decode_state_response(&self, response: &[u8]) -> Result<OpaqueStateResponse, String>;
fn poll<'a>(
&mut self,
cx: &mut std::task::Context<'a>,
) -> Poll<PollBlockAnnounceValidation<Block::Header>>;
) -> Poll<PollResult<Block>>;
fn send_block_request(
&mut self,
who: PeerId,
request: BlockRequest<Block>,
);
}
}
@@ -16,13 +16,16 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use sc_network_common::service::{NetworkPeers, NetworkSyncForkRequest};
use sp_runtime::traits::{Block as BlockT, NumberFor};
pub use libp2p::{identity::error::SigningError, kad::record::Key as KademliaKey};
use futures::channel::oneshot;
use libp2p::{Multiaddr, PeerId};
use sc_network_common::{config::MultiaddrWithPeerId, protocol::ProtocolName};
use sc_network_common::{
config::MultiaddrWithPeerId,
protocol::ProtocolName,
request_responses::{IfDisconnected, RequestFailure},
service::{NetworkPeers, NetworkRequest, NetworkSyncForkRequest},
};
use sc_peerset::ReputationChange;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::collections::HashSet;
mockall::mock! {
@@ -72,4 +75,23 @@ mockall::mock! {
fn remove_from_peers_set(&self, protocol: ProtocolName, peers: Vec<PeerId>);
fn sync_num_connected(&self) -> usize;
}
#[async_trait::async_trait]
impl NetworkRequest for Network {
async fn request(
&self,
target: PeerId,
protocol: ProtocolName,
request: Vec<u8>,
connect: IfDisconnected,
) -> Result<Vec<u8>, RequestFailure>;
fn start_request(
&self,
target: PeerId,
protocol: ProtocolName,
request: Vec<u8>,
tx: oneshot::Sender<Result<Vec<u8>, RequestFailure>>,
connect: IfDisconnected,
);
}
}
@@ -16,17 +16,21 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use futures::StreamExt;
use futures::{channel::oneshot, StreamExt};
use libp2p::PeerId;
use sc_network_common::{protocol::ProtocolName, service::NetworkPeers};
use sc_network_common::{
protocol::ProtocolName,
request_responses::{IfDisconnected, RequestFailure},
service::{NetworkPeers, NetworkRequest},
};
use sc_peerset::ReputationChange;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::sync::Arc;
/// Network-related services required by `sc-network-sync`
pub trait Network: NetworkPeers {}
pub trait Network: NetworkPeers + NetworkRequest {}
impl<T> Network for T where T: NetworkPeers {}
impl<T> Network for T where T: NetworkPeers + NetworkRequest {}
/// Network service provider for `ChainSync`
///
@@ -43,6 +47,15 @@ pub enum ToServiceCommand {
/// Call `NetworkPeers::report_peer()`
ReportPeer(PeerId, ReputationChange),
/// Call `NetworkRequest::start_request()`
StartRequest(
PeerId,
ProtocolName,
Vec<u8>,
oneshot::Sender<Result<Vec<u8>, RequestFailure>>,
IfDisconnected,
),
}
/// Handle that is (temporarily) passed to `ChainSync` so it can
@@ -67,6 +80,20 @@ impl NetworkServiceHandle {
pub fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
let _ = self.tx.unbounded_send(ToServiceCommand::DisconnectPeer(who, protocol));
}
/// Send request to peer
pub fn start_request(
&self,
who: PeerId,
protocol: ProtocolName,
request: Vec<u8>,
tx: oneshot::Sender<Result<Vec<u8>, RequestFailure>>,
connect: IfDisconnected,
) {
let _ = self
.tx
.unbounded_send(ToServiceCommand::StartRequest(who, protocol, request, tx, connect));
}
}
impl NetworkServiceProvider {
@@ -85,6 +112,8 @@ impl NetworkServiceProvider {
service.disconnect_peer(peer, protocol_name),
ToServiceCommand::ReportPeer(peer, reputation_change) =>
service.report_peer(peer, reputation_change),
ToServiceCommand::StartRequest(peer, protocol, request, tx, connect) =>
service.start_request(peer, protocol, request, tx, connect),
}
}
}
+16 -2
View File
@@ -19,7 +19,15 @@
use crate::{service::network::NetworkServiceProvider, ChainSync, ForkTarget};
use libp2p::PeerId;
use sc_network_common::{service::NetworkSyncForkRequest, sync::ChainSync as ChainSyncT};
use sc_network_common::{
config::ProtocolId,
protocol::{
role::{Role, Roles},
ProtocolName,
},
service::NetworkSyncForkRequest,
sync::ChainSync as ChainSyncT,
};
use sp_consensus::block_validation::DefaultBlockAnnounceValidator;
use sp_core::H256;
use std::{sync::Arc, task::Poll};
@@ -30,13 +38,19 @@ use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt as _
#[async_std::test]
async fn delegate_to_chainsync() {
let (_chain_sync_network_provider, chain_sync_network_handle) = NetworkServiceProvider::new();
let (mut chain_sync, chain_sync_service) = ChainSync::new(
let (mut chain_sync, chain_sync_service, _) = ChainSync::new(
sc_network_common::sync::SyncMode::Full,
Arc::new(TestClientBuilder::with_default_backend().build_with_longest_chain().0),
ProtocolId::from("test-protocol-name"),
&Some(String::from("test-fork-id")),
Roles::from(&Role::Full),
Box::new(DefaultBlockAnnounceValidator),
1u32,
None,
chain_sync_network_handle,
ProtocolName::from("block-request"),
ProtocolName::from("state-request"),
None,
)
.unwrap();