use std::{
collections::{HashMap, HashSet, VecDeque},
future::Future,
time::Duration,
vec,
};
use color_eyre::Report;
use indexmap::IndexSet;
use tokio::{pin, select, sync::mpsc, time::timeout};
use zebra_chain::{
block::{Hash, Height},
chain_tip::mock::MockChainTip,
};
use zebra_network as zn;
use zebra_state as zs;
use zebra_test::mock_service::{MockService, PanicAssertion};
use crate::{
components::sync::{ChainSync, FANOUT},
config::ZebradConfig,
};
mod known_hash_strategy;
mod prop;
type Mock<Req, Resp> = MockService<Req, Resp, PanicAssertion>;
struct TestScenario {
sync: ChainSync<
Mock<zn::Request, zn::Response>,
Mock<zs::Request, zs::Response>,
Mock<zs::ReadRequest, zs::ReadResponse>,
Mock<zebra_consensus::Request, Hash>,
MockChainTip,
>,
peers: Mock<zn::Request, zn::Response>,
state: Mock<zs::Request, zs::Response>,
}
impl TestScenario {
fn new() -> Self {
let peers = MockService::build().for_unit_tests();
let state = MockService::build().for_unit_tests();
let (tip, _) = MockChainTip::new();
let (misbehavior, _) = mpsc::channel(1);
let (sync, _) = ChainSync::new(
&ZebradConfig::default(),
Height(0),
peers.clone(),
MockService::build().for_unit_tests(),
state.clone(),
MockService::build().for_unit_tests(),
tip,
misbehavior,
);
Self { sync, peers, state }
}
async fn obtain_tips_with_hashes(
&mut self,
hashes: Vec<Hash>,
known: impl FnMut(Hash, usize) -> Option<zs::KnownBlock>,
) -> Result<IndexSet<Hash>, Report> {
self.obtain_tips_with_responses(vec![hashes], known).await
}
async fn obtain_tips_with_responses(
&mut self,
responses: Vec<Vec<Hash>>,
known: impl FnMut(Hash, usize) -> Option<zs::KnownBlock>,
) -> Result<IndexSet<Hash>, Report> {
let operation = DiscoveryResponses::new(responses, known).run(
self.sync.obtain_tips(),
&mut self.peers,
&mut self.state,
);
timeout(Duration::from_secs(10), operation)
.await
.expect("mocked discovery must finish")
}
}
struct DiscoveryResponses<Known> {
known: Known,
observations: HashMap<Hash, usize>,
responses: vec::IntoIter<Vec<Hash>>,
pending_initial_scan: VecDeque<Hash>,
remaining_responses: usize,
}
impl<Known> DiscoveryResponses<Known>
where
Known: FnMut(Hash, usize) -> Option<zs::KnownBlock>,
{
fn new(responses: Vec<Vec<Hash>>, known: Known) -> Self {
assert!(
responses.len() <= FANOUT,
"responses must fit the discovery fanout"
);
Self {
known,
observations: HashMap::new(),
responses: responses.into_iter(),
pending_initial_scan: VecDeque::new(),
remaining_responses: FANOUT,
}
}
async fn run(
mut self,
obtain: impl Future<Output = Result<IndexSet<Hash>, Report>>,
peers: &mut Mock<zn::Request, zn::Response>,
state: &mut Mock<zs::Request, zs::Response>,
) -> Result<IndexSet<Hash>, Report> {
pin!(obtain);
loop {
select! {
biased;
result = &mut obtain => return result,
request = state.expect_request_that(|_| true) => {
request.respond_with(|request| self.state_response(request));
}
request = peers.expect_request_that(|request| {
matches!(request, zn::Request::FindBlocks { .. })
}), if self.remaining_responses > 0 && self.pending_initial_scan.is_empty() => {
request.respond(self.peer_response());
}
}
}
}
fn state_response(&mut self, request: &zs::Request) -> zs::Response {
match request {
zs::Request::BlockLocator => zs::Response::BlockLocator(vec![Hash([0; 32])]),
zs::Request::KnownBlock(hash) => {
let location = self.known_block(*hash);
if let Some(expected_hash) = self.pending_initial_scan.pop_front() {
assert_eq!(*hash, expected_hash, "responses must be scanned in order");
if location.is_none() || location == Some(zs::KnownBlock::Queue) {
self.pending_initial_scan.clear();
}
}
zs::Response::KnownBlock(location)
}
request => panic!("unexpected state request: {request:?}"),
}
}
fn peer_response(&mut self) -> zn::Response {
self.remaining_responses -= 1;
let hashes = self.responses.next().unwrap_or_default();
self.pending_initial_scan = VecDeque::from(hashes.clone());
zn::Response::BlockHashes(hashes)
}
fn known_block(&mut self, hash: Hash) -> Option<zs::KnownBlock> {
let count = self.observations.entry(hash).or_insert(0);
*count += 1;
(self.known)(hash, *count)
}
}
async fn assert_downloaded_hashes(test: &mut TestScenario, hashes: impl IntoIterator<Item = Hash>) {
let mut remaining_hashes = HashSet::<Hash>::from_iter(hashes);
assert_eq!(test.sync.downloads.in_flight(), remaining_hashes.len());
for _ in 0..remaining_hashes.len() {
test.peers
.expect_request_that(|request| {
matches!(request, zn::Request::BlockDownload { hash, advertiser: None }
if remaining_hashes.remove(hash))
})
.await
.respond(Err(zn::BoxError::from(
"download not needed for discovery test",
)));
}
assert!(
remaining_hashes.is_empty(),
"missing downloads: {remaining_hashes:?}"
);
}