Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Introduce mockable ChainSync object for testing (#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 <[email protected]>

* Update Cargo.lock

* Fix clippy and documentation

Co-authored-by: Bastian Köcher <[email protected]>
Co-authored-by: parity-processbot <>
  • Loading branch information
altonen and bkchr authored Oct 13, 2022
1 parent 94941a8 commit 983b6b0
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 983b6b0

Please sign in to comment.