Skip to content

Commit

Permalink
Better assert message in lookup sampling test
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Oct 8, 2024
1 parent 48dd3f3 commit 81f4603
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
38 changes: 27 additions & 11 deletions beacon_node/network/src/sync/block_lookups/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,14 +1303,30 @@ impl TestRig {
});
}

fn assert_sampling_request_status(
&self,
block_root: Hash256,
ongoing: &Vec<ColumnIndex>,
no_peers: &Vec<ColumnIndex>,
) {
self.sync_manager
.assert_sampling_request_status(block_root, ongoing, no_peers)
fn assert_sampling_request_ongoing(&self, block_root: Hash256, indices: &Vec<ColumnIndex>) {
for index in indices {
let status = self
.sync_manager
.get_sampling_request_status(block_root, index)
.unwrap_or_else(|| panic!("No request state for {index}"));
assert_eq!(
status, "Sampling",
"expected {block_root} {index} request to be ongoing"
);
}
}

fn assert_sampling_request_nopeers(&self, block_root: Hash256, indices: &Vec<ColumnIndex>) {
for index in indices {
let status = self
.sync_manager
.get_sampling_request_status(block_root, index)
.unwrap_or_else(|| panic!("No request state for {index}"));
assert_eq!(
status, "NoPeers",
"expected {block_root} {index} request to be nopeers"
);
}
}
}

Expand Down Expand Up @@ -2061,7 +2077,7 @@ fn sampling_batch_requests() {
.pop()
.unwrap();
assert_eq!(column_indexes.len(), SAMPLING_REQUIRED_SUCCESSES);
r.assert_sampling_request_status(block_root, &column_indexes, &vec![]);
r.assert_sampling_request_ongoing(block_root, &column_indexes);

// Resolve the request.
r.complete_valid_sampling_column_requests(
Expand Down Expand Up @@ -2089,7 +2105,7 @@ fn sampling_batch_requests_not_enough_responses_returned() {
assert_eq!(column_indexes.len(), SAMPLING_REQUIRED_SUCCESSES);

// The request status should be set to Sampling.
r.assert_sampling_request_status(block_root, &column_indexes, &vec![]);
r.assert_sampling_request_ongoing(block_root, &column_indexes);

// Split the indexes to simulate the case where the supernode doesn't have the requested column.
let (_column_indexes_supernode_does_not_have, column_indexes_to_complete) =
Expand All @@ -2107,7 +2123,7 @@ fn sampling_batch_requests_not_enough_responses_returned() {
);

// The request status should be set to NoPeers since the supernode, the only peer, returned not enough responses.
r.assert_sampling_request_status(block_root, &vec![], &column_indexes);
r.assert_sampling_request_nopeers(block_root, &column_indexes);

// The sampling request stalls.
r.expect_empty_network();
Expand Down
10 changes: 4 additions & 6 deletions beacon_node/network/src/sync/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,12 @@ impl<T: BeaconChainTypes> SyncManager<T> {
}

#[cfg(test)]
pub(crate) fn assert_sampling_request_status(
pub(crate) fn get_sampling_request_status(
&self,
block_root: Hash256,
ongoing: &Vec<ColumnIndex>,
no_peers: &Vec<ColumnIndex>,
) {
self.sampling
.assert_sampling_request_status(block_root, ongoing, no_peers);
index: &ColumnIndex,
) -> Option<&'static str> {
self.sampling.get_request_status(block_root, index)
}

fn network_globals(&self) -> &NetworkGlobals<T::EthSpec> {
Expand Down
33 changes: 12 additions & 21 deletions beacon_node/network/src/sync/peer_sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl<T: BeaconChainTypes> Sampling<T> {
}

#[cfg(test)]
pub fn assert_sampling_request_status(
pub fn get_request_status(
&self,
block_root: Hash256,
ongoing: &Vec<ColumnIndex>,
no_peers: &Vec<ColumnIndex>,
) {
index: &ColumnIndex,
) -> Option<&'static str> {
let requester = SamplingRequester::ImportedBlock(block_root);
let active_sampling_request = self.requests.get(&requester).unwrap();
active_sampling_request.assert_sampling_request_status(ongoing, no_peers);
self.requests
.get(&requester)
.and_then(|req| req.get_request_status(index))
}

/// Create a new sampling request for a known block
Expand Down Expand Up @@ -233,18 +233,8 @@ impl<T: BeaconChainTypes> ActiveSamplingRequest<T> {
}

#[cfg(test)]
pub fn assert_sampling_request_status(
&self,
ongoing: &Vec<ColumnIndex>,
no_peers: &Vec<ColumnIndex>,
) {
for idx in ongoing {
assert!(self.column_requests.get(idx).unwrap().is_ongoing());
}

for idx in no_peers {
assert!(self.column_requests.get(idx).unwrap().is_no_peers());
}
pub fn get_request_status(&self, index: &ColumnIndex) -> Option<&'static str> {
self.column_requests.get(index).map(|req| req.status_str())
}

/// Insert a downloaded column into an active sampling request. Then make progress on the
Expand Down Expand Up @@ -575,6 +565,7 @@ mod request {
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::collections::HashSet;
use strum::IntoStaticStr;
use types::data_column_sidecar::ColumnIndex;

pub(crate) struct ActiveColumnSampleRequest {
Expand All @@ -584,7 +575,7 @@ mod request {
peers_dont_have: HashSet<PeerId>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, IntoStaticStr)]
enum Status {
NoPeers,
NotStarted,
Expand Down Expand Up @@ -630,8 +621,8 @@ mod request {
}

#[cfg(test)]
pub(crate) fn is_no_peers(&self) -> bool {
matches!(self.status, Status::NoPeers)
pub(crate) fn status_str(&self) -> &'static str {
self.status.clone().into()
}

pub(crate) fn choose_peer<T: BeaconChainTypes>(
Expand Down

0 comments on commit 81f4603

Please sign in to comment.