Skip to content

Commit

Permalink
Extract block announce validation from ChainSync (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-markin authored and Ank4n committed Sep 8, 2023
1 parent daf6141 commit c08d14d
Show file tree
Hide file tree
Showing 7 changed files with 727 additions and 546 deletions.
68 changes: 7 additions & 61 deletions substrate/client/network/common/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ pub mod message;
pub mod metrics;
pub mod warp;

use crate::{role::Roles, types::ReputationChange};
use crate::{role::Roles, sync::message::BlockAnnounce, types::ReputationChange};
use futures::Stream;

use libp2p_identity::PeerId;

use message::{BlockAnnounce, BlockData, BlockRequest, BlockResponse};
use message::{BlockData, BlockRequest, BlockResponse};
use sc_consensus::{import_queue::RuntimeOrigin, IncomingBlock};
use sp_consensus::BlockOrigin;
use sp_runtime::{
Expand Down Expand Up @@ -157,38 +157,6 @@ pub enum ImportResult<B: BlockT> {
JustificationImport(RuntimeOrigin, B::Hash, NumberFor<B>, Justifications),
}

/// Value polled from `ChainSync`
#[derive(Debug)]
pub enum PollResult<B: BlockT> {
Import(ImportResult<B>),
Announce(PollBlockAnnounceValidation<B::Header>),
}

/// Result of [`ChainSync::poll_block_announce_validation`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PollBlockAnnounceValidation<H> {
/// The announcement failed at validation.
///
/// The peer reputation should be decreased.
Failure {
/// Who sent the processed block announcement?
who: PeerId,
/// Should the peer be disconnected?
disconnect: bool,
},
/// The announcement does not require further handling.
Nothing {
/// Who sent the processed block announcement?
who: PeerId,
/// Was this their new best block?
is_best: bool,
/// The announcement.
announce: BlockAnnounce<H>,
},
/// The block announcement should be skipped.
Skip,
}

/// Sync operation mode.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SyncMode {
Expand Down Expand Up @@ -408,29 +376,14 @@ pub trait ChainSync<Block: BlockT>: Send {
/// Notify about finalization of the given block.
fn on_block_finalized(&mut self, hash: &Block::Hash, number: NumberFor<Block>);

/// Push a block announce validation.
///
/// It is required that [`ChainSync::poll_block_announce_validation`] is called
/// to check for finished block announce validations.
fn push_block_announce_validation(
/// Notify about pre-validated block announcement.
fn on_validated_block_announce(
&mut self,
who: PeerId,
hash: Block::Hash,
announce: BlockAnnounce<Block::Header>,
is_best: bool,
who: PeerId,
announce: &BlockAnnounce<Block::Header>,
);

/// Poll block announce validation.
///
/// Block announce validations can be pushed by using
/// [`ChainSync::push_block_announce_validation`].
///
/// This should be polled until it returns [`Poll::Pending`].
fn poll_block_announce_validation(
&mut self,
cx: &mut std::task::Context<'_>,
) -> Poll<PollBlockAnnounceValidation<Block::Header>>;

/// Call when a peer has disconnected.
/// Canceled obsolete block request may result in some blocks being ready for
/// import, so this functions checks for such blocks and returns them.
Expand All @@ -447,14 +400,7 @@ pub trait ChainSync<Block: BlockT>: Send {
) -> Result<Vec<BlockData<Block>>, String>;

/// Advance the state of `ChainSync`
///
/// Internally calls [`ChainSync::poll_block_announce_validation()`] and
/// this function should be polled until it returns [`Poll::Pending`] to
/// consume all pending events.
fn poll(
&mut self,
cx: &mut std::task::Context,
) -> Poll<PollBlockAnnounceValidation<Block::Header>>;
fn poll(&mut self, cx: &mut std::task::Context) -> Poll<()>;

/// Send block request to peer
fn send_block_request(&mut self, who: PeerId, request: BlockRequest<Block>);
Expand Down
Loading

0 comments on commit c08d14d

Please sign in to comment.