Unify ChainSync actions under one enum (follow-up) (#2317)

Get rid of public `ChainSync::..._requests()` functions and return all
requests as actions.

---------

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
This commit is contained in:
Dmitry Markin
2023-11-15 10:33:58 +02:00
committed by GitHub
parent f53604362c
commit 18165ebba8
7 changed files with 74 additions and 47 deletions
+40 -10
View File
@@ -191,6 +191,10 @@ pub enum ChainSyncAction<B: BlockT> {
SendBlockRequest { peer_id: PeerId, request: BlockRequest<B> },
/// Drop stale block request.
CancelBlockRequest { peer_id: PeerId },
/// Send state request to peer.
SendStateRequest { peer_id: PeerId, request: OpaqueStateRequest },
/// Send warp proof request to peer.
SendWarpProofRequest { peer_id: PeerId, request: WarpProofRequest<B> },
/// Peer misbehaved. Disconnect, report it and cancel the block request to it.
DropPeer(BadPeer),
/// Import blocks.
@@ -1420,11 +1424,6 @@ where
.any(|(_, p)| p.state == PeerSyncState::DownloadingStale(*hash))
}
/// Check if the peer is known to the sync state machine. Used for sanity checks.
pub fn is_peer_known(&self, peer_id: &PeerId) -> bool {
self.peers.contains_key(peer_id)
}
/// Get the set of downloaded blocks that are ready to be queued for import.
fn ready_blocks(&mut self) -> Vec<IncomingBlock<B>> {
self.blocks
@@ -1537,7 +1536,7 @@ where
}
/// Get justification requests scheduled by sync to be sent out.
pub fn justification_requests(&mut self) -> Vec<(PeerId, BlockRequest<B>)> {
fn justification_requests(&mut self) -> Vec<(PeerId, BlockRequest<B>)> {
let peers = &mut self.peers;
let mut matcher = self.extra_justifications.matcher();
std::iter::from_fn(move || {
@@ -1564,7 +1563,7 @@ where
}
/// Get block requests scheduled by sync to be sent out.
pub fn block_requests(&mut self) -> Vec<(PeerId, BlockRequest<B>)> {
fn block_requests(&mut self) -> Vec<(PeerId, BlockRequest<B>)> {
if self.mode == SyncMode::Warp {
return self
.warp_target_block_request()
@@ -1691,7 +1690,7 @@ where
}
/// Get a state request scheduled by sync to be sent out (if any).
pub fn state_request(&mut self) -> Option<(PeerId, OpaqueStateRequest)> {
fn state_request(&mut self) -> Option<(PeerId, OpaqueStateRequest)> {
if self.allowed_requests.is_empty() {
return None
}
@@ -1737,7 +1736,7 @@ where
}
/// Get a warp proof request scheduled by sync to be sent out (if any).
pub fn warp_sync_request(&mut self) -> Option<(PeerId, WarpProofRequest<B>)> {
fn warp_sync_request(&mut self) -> Option<(PeerId, WarpProofRequest<B>)> {
if let Some(sync) = &self.warp_sync {
if self.allowed_requests.is_empty() ||
sync.is_complete() ||
@@ -2025,7 +2024,38 @@ where
/// Get pending actions to perform.
#[must_use]
pub fn take_actions(&mut self) -> impl Iterator<Item = ChainSyncAction<B>> {
pub fn actions(&mut self) -> impl Iterator<Item = ChainSyncAction<B>> {
let block_requests = self
.block_requests()
.into_iter()
.map(|(peer_id, request)| ChainSyncAction::SendBlockRequest { peer_id, request });
self.actions.extend(block_requests);
let justification_requests = self
.justification_requests()
.into_iter()
.map(|(peer_id, request)| ChainSyncAction::SendBlockRequest { peer_id, request });
self.actions.extend(justification_requests);
let state_request = self
.state_request()
.into_iter()
.map(|(peer_id, request)| ChainSyncAction::SendStateRequest { peer_id, request });
self.actions.extend(state_request);
let warp_proof_request = self
.warp_sync_request()
.into_iter()
.map(|(peer_id, request)| ChainSyncAction::SendWarpProofRequest { peer_id, request });
self.actions.extend(warp_proof_request);
std::mem::take(&mut self.actions).into_iter()
}
/// A version of `actions()` that doesn't schedule extra requests. For testing only.
#[cfg(test)]
#[must_use]
fn take_actions(&mut self) -> impl Iterator<Item = ChainSyncAction<B>> {
std::mem::take(&mut self.actions).into_iter()
}
}