Skip to content

Commit 3e3a998

Browse files
Extract block announce validation from ChainSync (paritytech#1170)
1 parent 2675915 commit 3e3a998

File tree

7 files changed

+727
-546
lines changed

7 files changed

+727
-546
lines changed

substrate/client/network/common/src/sync.rs

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ pub mod message;
2222
pub mod metrics;
2323
pub mod warp;
2424

25-
use crate::{role::Roles, types::ReputationChange};
25+
use crate::{role::Roles, sync::message::BlockAnnounce, types::ReputationChange};
2626
use futures::Stream;
2727

2828
use libp2p_identity::PeerId;
2929

30-
use message::{BlockAnnounce, BlockData, BlockRequest, BlockResponse};
30+
use message::{BlockData, BlockRequest, BlockResponse};
3131
use sc_consensus::{import_queue::RuntimeOrigin, IncomingBlock};
3232
use sp_consensus::BlockOrigin;
3333
use sp_runtime::{
@@ -157,38 +157,6 @@ pub enum ImportResult<B: BlockT> {
157157
JustificationImport(RuntimeOrigin, B::Hash, NumberFor<B>, Justifications),
158158
}
159159

160-
/// Value polled from `ChainSync`
161-
#[derive(Debug)]
162-
pub enum PollResult<B: BlockT> {
163-
Import(ImportResult<B>),
164-
Announce(PollBlockAnnounceValidation<B::Header>),
165-
}
166-
167-
/// Result of [`ChainSync::poll_block_announce_validation`].
168-
#[derive(Debug, Clone, PartialEq, Eq)]
169-
pub enum PollBlockAnnounceValidation<H> {
170-
/// The announcement failed at validation.
171-
///
172-
/// The peer reputation should be decreased.
173-
Failure {
174-
/// Who sent the processed block announcement?
175-
who: PeerId,
176-
/// Should the peer be disconnected?
177-
disconnect: bool,
178-
},
179-
/// The announcement does not require further handling.
180-
Nothing {
181-
/// Who sent the processed block announcement?
182-
who: PeerId,
183-
/// Was this their new best block?
184-
is_best: bool,
185-
/// The announcement.
186-
announce: BlockAnnounce<H>,
187-
},
188-
/// The block announcement should be skipped.
189-
Skip,
190-
}
191-
192160
/// Sync operation mode.
193161
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
194162
pub enum SyncMode {
@@ -408,29 +376,14 @@ pub trait ChainSync<Block: BlockT>: Send {
408376
/// Notify about finalization of the given block.
409377
fn on_block_finalized(&mut self, hash: &Block::Hash, number: NumberFor<Block>);
410378

411-
/// Push a block announce validation.
412-
///
413-
/// It is required that [`ChainSync::poll_block_announce_validation`] is called
414-
/// to check for finished block announce validations.
415-
fn push_block_announce_validation(
379+
/// Notify about pre-validated block announcement.
380+
fn on_validated_block_announce(
416381
&mut self,
417-
who: PeerId,
418-
hash: Block::Hash,
419-
announce: BlockAnnounce<Block::Header>,
420382
is_best: bool,
383+
who: PeerId,
384+
announce: &BlockAnnounce<Block::Header>,
421385
);
422386

423-
/// Poll block announce validation.
424-
///
425-
/// Block announce validations can be pushed by using
426-
/// [`ChainSync::push_block_announce_validation`].
427-
///
428-
/// This should be polled until it returns [`Poll::Pending`].
429-
fn poll_block_announce_validation(
430-
&mut self,
431-
cx: &mut std::task::Context<'_>,
432-
) -> Poll<PollBlockAnnounceValidation<Block::Header>>;
433-
434387
/// Call when a peer has disconnected.
435388
/// Canceled obsolete block request may result in some blocks being ready for
436389
/// import, so this functions checks for such blocks and returns them.
@@ -447,14 +400,7 @@ pub trait ChainSync<Block: BlockT>: Send {
447400
) -> Result<Vec<BlockData<Block>>, String>;
448401

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

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

0 commit comments

Comments
 (0)