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

Commit

Permalink
Fix tests and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
tdimitrov committed May 20, 2022
1 parent 1dfd12e commit 40a11c9
Showing 1 changed file with 4 additions and 202 deletions.
206 changes: 4 additions & 202 deletions node/core/provisioner/src/disputes/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ fn normal_flow() {
disputes.add_concluded_disputes_unknown_onchain(DISPUTES_PER_BATCH);

// concluded disputes known onchain
let (fourth_idx, fourth_votes) =
let (fourth_idx, _) =
disputes.add_concluded_disputes_known_onchain(DISPUTES_PER_BATCH);

// active disputes unknown onchain
Expand All @@ -451,9 +451,9 @@ fn normal_flow() {
let disputes = select_disputes(&mut tx, &metrics, &lf).await.unwrap();

assert!(!disputes.is_empty());
assert_eq!(disputes.len(), 5 * DISPUTES_PER_BATCH);

if cfg!(feature = "staging-client") {
assert_eq!(disputes.len(), 4 * DISPUTES_PER_BATCH);
for i in 0..DISPUTES_PER_BATCH {
assert_eq!(disputes.get(i).unwrap().session, first_idx);
}
Expand All @@ -464,7 +464,7 @@ fn normal_flow() {
assert_eq!(disputes.get(i).unwrap().session, third_idx);
}
for i in 3 * DISPUTES_PER_BATCH..4 * DISPUTES_PER_BATCH {
assert_eq!(disputes.get(i).unwrap().session, fourth_idx);
assert_eq!(disputes.get(i).unwrap().session, fifth_idx);
}

assert_eq!(
Expand Down Expand Up @@ -493,16 +493,10 @@ fn normal_flow() {
.iter()
.map(|d| d.statements.len())
.fold(0, |acc, v| acc + v),
fourth_votes
);
assert_eq!(
disputes[4 * DISPUTES_PER_BATCH..5 * DISPUTES_PER_BATCH]
.iter()
.map(|d| d.statements.len())
.fold(0, |acc, v| acc + v),
fifth_votes
);
} else {
assert_eq!(disputes.len(), 5 * DISPUTES_PER_BATCH);
for i in 0..3 * DISPUTES_PER_BATCH {
assert_ne!(disputes.get(i).unwrap().session, fourth_idx);
assert_ne!(disputes.get(i).unwrap().session, fifth_idx);
Expand All @@ -512,195 +506,3 @@ fn normal_flow() {
);
assert!(vote_queries < ACCEPTABLE_RUNTIME_VOTES_QUERIES_COUNT);
}

// Global Test Data
//These tests rely on staging Runtime functions so they are separated and compiled conditionally.
#[cfg(feature = "staging-client__")]
mod staging_tests {
use super::*;

// fn dummy_dispute_state() -> DisputeState {
// DisputeState {
// validators_for: BitVec::new(),
// validators_against: BitVec::new(),
// start: 0,
// concluded_at: None,
// }
// }

#[test]
fn recent_disputes_are_too_much_active_fits_test_recent_prioritisation() {
// In this case recent disputes are above `MAX_DISPUTES_FORWARDED_TO_RUNTIME` limit and the active ones are below it.
// The expected behaviour is to send all active disputes and extend the set with recent ones. During the extension the disputes unknown for the Runtime are added with priority.
const RECENT_DISPUTES_SIZE: usize = MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME + 10;
const ACTIVE_DISPUTES_SIZE: usize = MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME - 10;
const ONCHAIN_DISPUTE_SIZE: usize = RECENT_DISPUTES_SIZE - 9;
let metrics = metrics::Metrics::new_dummy();
let recent_disputes = recent_disputes(RECENT_DISPUTES_SIZE);
let active_disputes = active_disputes(ACTIVE_DISPUTES_SIZE);
let onchain_disputes: Result<
Vec<(SessionIndex, CandidateHash, DisputeState)>,
RuntimeApiError,
> = Ok(Vec::from(&recent_disputes[0..ONCHAIN_DISPUTE_SIZE])
.iter()
.map(|(session_index, candidate_hash)| {
(*session_index, candidate_hash.clone(), dummy_dispute_state())
})
.collect());
let active_disputes_overseer = active_disputes.clone();
let recent_disputes_overseer = recent_disputes.clone();
test_harness(
|r| {
mock_overseer(
leaf(),
r,
onchain_disputes,
recent_disputes_overseer,
active_disputes_overseer,
)
},
|mut tx: TestSubsystemSender| async move {
let lf = leaf();
let disputes = select_disputes(&mut tx, &metrics, &lf).await.unwrap();

assert!(!disputes.is_empty());

// Recent disputes are generated with `SessionIndex` = 0
let (res_recent, res_active): (Vec<DisputeStatementSet>, Vec<DisputeStatementSet>) =
disputes.into_iter().partition(|d| d.session == 0);

// It should be good enough the count the number of the disputes and not compare them one by one as this was already covered in other tests.
assert_eq!(res_active.len(), active_disputes.len()); // We've got all active disputes.
assert_eq!(
res_recent.len(),
MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME - active_disputes.len()
); // And some recent ones.

// Check if the recent disputes were unknown for the Runtime.
let expected_recent_disputes = Vec::from(&recent_disputes[ONCHAIN_DISPUTE_SIZE..]);
let res_recent_set: HashSet<(SessionIndex, CandidateHash)> =
HashSet::from_iter(res_recent.iter().map(|d| (d.session, d.candidate_hash)));

// Explicitly check that all unseen disputes are sent to the Runtime.
for d in &expected_recent_disputes {
assert_eq!(res_recent_set.contains(d), true);
}
},
)
}

#[test]
fn active_disputes_are_too_much_test_active_prioritisation() {
// In this case the active disputes are above the `MAX_DISPUTES_FORWARDED_TO_RUNTIME` limit so the unseen ones should be prioritised.
const RECENT_DISPUTES_SIZE: usize = MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME + 10;
const ACTIVE_DISPUTES_SIZE: usize = MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME + 10;
const ONCHAIN_DISPUTE_SIZE: usize = ACTIVE_DISPUTES_SIZE - 9;

let metrics = metrics::Metrics::new_dummy();
let recent_disputes = recent_disputes(RECENT_DISPUTES_SIZE);
let active_disputes = active_disputes(ACTIVE_DISPUTES_SIZE);
let onchain_disputes: Result<
Vec<(SessionIndex, CandidateHash, DisputeState)>,
RuntimeApiError,
> = Ok(Vec::from(&active_disputes[0..ONCHAIN_DISPUTE_SIZE])
.iter()
.map(|(session_index, candidate_hash)| {
(*session_index, candidate_hash.clone(), dummy_dispute_state())
})
.collect());
let active_disputes_overseer = active_disputes.clone();
let recent_disputes_overseer = recent_disputes.clone();
test_harness(
|r| {
mock_overseer(
leaf(),
r,
onchain_disputes,
recent_disputes_overseer,
active_disputes_overseer,
)
},
|mut tx: TestSubsystemSender| async move {
let lf = leaf();
let disputes = select_disputes(&mut tx, &metrics, &lf).await.unwrap();

assert!(!disputes.is_empty());

// Recent disputes are generated with `SessionIndex` = 0
let (res_recent, res_active): (Vec<DisputeStatementSet>, Vec<DisputeStatementSet>) =
disputes.into_iter().partition(|d| d.session == 0);

// It should be good enough the count the number of the disputes and not compare them one by one
assert_eq!(res_recent.len(), 0); // We expect no recent disputes
assert_eq!(res_active.len(), MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME);

let expected_active_disputes = Vec::from(&active_disputes[ONCHAIN_DISPUTE_SIZE..]);
let res_active_set: HashSet<(SessionIndex, CandidateHash)> =
HashSet::from_iter(res_active.iter().map(|d| (d.session, d.candidate_hash)));

// Explicitly check that the unseen disputes are delivered to the Runtime.
for d in &expected_active_disputes {
assert_eq!(res_active_set.contains(d), true);
}
},
)
}

#[test]
fn active_disputes_are_too_much_and_are_all_unseen() {
// In this case there are a lot of active disputes unseen by the Runtime. The focus of the test is to verify that in such cases known disputes are NOT sent to the Runtime.
const RECENT_DISPUTES_SIZE: usize = MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME + 10;
const ACTIVE_DISPUTES_SIZE: usize = MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME + 5;
const ONCHAIN_DISPUTE_SIZE: usize = 5;

let metrics = metrics::Metrics::new_dummy();
let recent_disputes = recent_disputes(RECENT_DISPUTES_SIZE);
let active_disputes = active_disputes(ACTIVE_DISPUTES_SIZE);
let onchain_disputes: Result<
Vec<(SessionIndex, CandidateHash, DisputeState)>,
RuntimeApiError,
> = Ok(Vec::from(&active_disputes[0..ONCHAIN_DISPUTE_SIZE])
.iter()
.map(|(session_index, candidate_hash)| {
(*session_index, candidate_hash.clone(), dummy_dispute_state())
})
.collect());
let active_disputes_overseer = active_disputes.clone();
let recent_disputes_overseer = recent_disputes.clone();
test_harness(
|r| {
mock_overseer(
leaf(),
r,
onchain_disputes,
recent_disputes_overseer,
active_disputes_overseer,
)
},
|mut tx: TestSubsystemSender| async move {
let lf = leaf();
let disputes = select_disputes(&mut tx, &metrics, &lf).await.unwrap();
assert!(!disputes.is_empty());

// Recent disputes are generated with `SessionIndex` = 0
let (res_recent, res_active): (Vec<DisputeStatementSet>, Vec<DisputeStatementSet>) =
disputes.into_iter().partition(|d| d.session == 0);

// It should be good enough the count the number of the disputes and not compare them one by one
assert_eq!(res_recent.len(), 0);
assert_eq!(res_active.len(), MAX_DISPUTE_VOTES_FORWARDED_TO_RUNTIME);

// For sure we don't want to see any of this disputes in the result
let unexpected_active_disputes =
Vec::from(&active_disputes[0..ONCHAIN_DISPUTE_SIZE]);
let res_active_set: HashSet<(SessionIndex, CandidateHash)> =
HashSet::from_iter(res_active.iter().map(|d| (d.session, d.candidate_hash)));

// Verify that the result DOESN'T contain known disputes (because there is an excessive number of unknown onces).
for d in &unexpected_active_disputes {
assert_eq!(res_active_set.contains(d), false);
}
},
)
}
}

0 comments on commit 40a11c9

Please sign in to comment.