Skip to content

Commit

Permalink
Introduce mockable ChainSync object for testing (paritytech#12480)
Browse files Browse the repository at this point in the history
* Introduce mockable `ChainSync` object for testing

`mockall` allows to mock `ChainSync` and to verify that the calls made
to `ChaiSync` are firstly executed at all, that they're executed in
correct order and with correct parameters.

This allows to verify, e.g., that delegating calls directly to
`ChainSync` from `NetworkService` still calls the correct functions with
correct arguments even if `Protocol` middleman is removed.

* Add Cargo.lock

* Fix tests

* Update client/network/Cargo.toml

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update Cargo.lock

* Fix clippy and documentation

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: parity-processbot <>
  • Loading branch information
2 people authored and ark0f committed Feb 27, 2023
1 parent 63af21f commit 6cfeaf3
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 23 deletions.
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions client/network/common/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ pub trait ChainSync<Block: BlockT>: Send {
);

/// Get an iterator over all scheduled justification requests.
fn justification_requests(
&mut self,
) -> Box<dyn Iterator<Item = (PeerId, BlockRequest<Block>)> + '_>;
fn justification_requests<'a>(
&'a mut self,
) -> Box<dyn Iterator<Item = (PeerId, BlockRequest<Block>)> + 'a>;

/// Get an iterator over all block requests of all peers.
fn block_requests(&mut self) -> Box<dyn Iterator<Item = (&PeerId, BlockRequest<Block>)> + '_>;
fn block_requests<'a>(
&'a mut self,
) -> Box<dyn Iterator<Item = (PeerId, BlockRequest<Block>)> + 'a>;

/// Get a state request, if any.
fn state_request(&mut self) -> Option<(PeerId, OpaqueStateRequest)>;
Expand Down Expand Up @@ -359,9 +361,9 @@ pub trait ChainSync<Block: BlockT>: Send {
///
/// If [`PollBlockAnnounceValidation::ImportHeader`] is returned, then the caller MUST try to
/// import passed header (call `on_block_data`). The network request isn't sent in this case.
fn poll_block_announce_validation(
fn poll_block_announce_validation<'a>(
&mut self,
cx: &mut std::task::Context,
cx: &mut std::task::Context<'a>,
) -> Poll<PollBlockAnnounceValidation<Block::Header>>;

/// Call when a peer has disconnected.
Expand Down
2 changes: 1 addition & 1 deletion client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ where
for (id, request) in self
.chain_sync
.block_requests()
.map(|(peer_id, request)| (*peer_id, request))
.map(|(peer_id, request)| (peer_id, request))
.collect::<Vec<_>>()
{
let event =
Expand Down
2 changes: 2 additions & 0 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ use std::{

pub use behaviour::{InboundFailure, OutboundFailure, ResponseFailure};

#[cfg(test)]
mod chainsync_tests;
mod metrics;
mod out_events;
#[cfg(test)]
Expand Down
Loading

0 comments on commit 6cfeaf3

Please sign in to comment.