Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract block announce validation from ChainSync #1170

Merged
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c8c1cf6
Extract block announce validation/polling from `ChainSync`
dmitry-markin Jul 27, 2023
0046f35
Get rid of `push_block_announce_validation_inner`
dmitry-markin Jul 27, 2023
d41acb3
Extend `ChainSync` trait with `on_validated_block_announce`
dmitry-markin Jul 28, 2023
72d6d69
minor: fix visibilities
dmitry-markin Jul 28, 2023
c52f076
Fix: poll `ChainSync` from `SyncingEngine`
dmitry-markin Jul 28, 2023
36a002e
minor: spelling
dmitry-markin Jul 28, 2023
e307f9a
Unify `PeerId` variable names as `peer_id`
dmitry-markin Jul 31, 2023
2186e0f
Assert when deallocating block announce validation slot counter
dmitry-markin Jul 31, 2023
c6461e0
Extract block announce validation from `SyncingEngine`
dmitry-markin Jul 31, 2023
5b6e10a
Simplify naming in `BlockAnnounceValidator`
dmitry-markin Jul 31, 2023
a67c9c8
Implement `Stream` trait for `BlockAnnounceValidator`
dmitry-markin Jul 31, 2023
32e95be
minor: simplify code a little
dmitry-markin Jul 31, 2023
c925e77
WIP: wake-up task if new validation is added to `BlockAnnounceValidator`
dmitry-markin Jul 31, 2023
51ff231
Wake up task when new validation is added to `BlockAnnounceValidator`
dmitry-markin Aug 1, 2023
936cf4f
Get rid of `PreValidateBlockAnnounce`
dmitry-markin Aug 1, 2023
1448225
Remove now unneeded polling after pushing block announce validation
dmitry-markin Aug 1, 2023
03597be
Add tests for validation slot allocation
dmitry-markin Aug 1, 2023
aa48154
minor: make rutdoc happy
dmitry-markin Aug 1, 2023
ec6b028
Apply suggestions from code review
dmitry-markin Aug 2, 2023
d242f5f
Use `futures::ready!` to simplify polling
dmitry-markin Aug 2, 2023
e42954f
Prepare `BloackAnnounceValidator` for pushing block announcements fro…
dmitry-markin Aug 24, 2023
859237b
minor: comment
dmitry-markin Aug 24, 2023
bf99c1c
minor: rustfmt
dmitry-markin Aug 24, 2023
14b76f1
Get rid of a channel in `FuturesStream`
dmitry-markin Aug 25, 2023
d728fe0
Merge remote-tracking branch 'origin/master' into dm-extract-block-an…
dmitry-markin Aug 25, 2023
c3f9ab6
minor: docs
dmitry-markin Aug 25, 2023
096c0e0
Merge remote-tracking branch 'origin/master' into dm-extract-block-an…
dmitry-markin Aug 28, 2023
ca546cd
Apply code review suggestions
dmitry-markin Aug 28, 2023
55046c5
minor: replace spaces with tabs
dmitry-markin Aug 28, 2023
5610da0
minor: comment
dmitry-markin Aug 28, 2023
43d8e56
Merge remote-tracking branch 'origin/master' into dm-extract-block-an…
dmitry-markin Aug 28, 2023
c9797a5
Fix possible underflow by using `saturating_sub`
dmitry-markin Aug 29, 2023
c8340e2
Merge remote-tracking branch 'origin/master' into dm-extract-block-an…
dmitry-markin Aug 29, 2023
04c1348
Merge remote-tracking branch 'origin/master' into dm-extract-block-an…
dmitry-markin Sep 1, 2023
d629420
Merge remote-tracking branch 'origin/master' into dm-extract-block-an…
dmitry-markin Sep 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading