Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup tests
  • Loading branch information
drahnr committed Jan 27, 2022
commit 6f5cdface24ab7c1550b746db6ffd4e43e02d612
119 changes: 89 additions & 30 deletions runtime/parachains/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ use super::paras_inherent::IsSortedBy;

/// Returns `true` if duplicate items were found, otherwise `false`.
///
/// `check_equal(a: &T, b: &T)` _must_ return `true`, iff `a` and `b` are equal, otherwise `false.
/// The definition of _equal_ is to be defined by the user.
///
/// Attention: Requires the input `iter` to be sorted, such that _equals_
/// would be adjacent in respect to `check_equal`!
fn check_for_duplicates_in_sorted_iter<
/// would be adjacent in respect whatever `check_equal` defines as equality!
fn contains_duplicates_in_sorted_iter<
'a,
T: 'a,
I: 'a + IntoIterator<Item = &'a T>,
Expand Down Expand Up @@ -190,7 +193,7 @@ pub trait DisputesHandler<BlockNumber: Ord> {
return Err(())
}
// Sorted, so according to session and candidate hash, this will detect duplicates.
if check_for_duplicates_in_sorted_iter(statement_sets, |previous, current| {
if contains_duplicates_in_sorted_iter(statement_sets, |previous, current| {
current.session == previous.session && current.candidate_hash == previous.candidate_hash
}) {
return Err(())
Expand Down Expand Up @@ -1390,13 +1393,13 @@ mod tests {
}

#[test]
fn test_check_for_duplicates_in_sorted_iter() {
fn test_contains_duplicates_in_sorted_iter() {
// We here use the implicit ascending sorting and builtin equality of integers
let v = vec![1, 2, 3, 5, 5, 8];
assert_eq!(true, check_for_duplicates_in_sorted_iter(&v, |a, b| a == b));
assert_eq!(true, contains_duplicates_in_sorted_iter(&v, |a, b| a == b));

let v = vec![1, 2, 3, 4];
assert_eq!(false, check_for_duplicates_in_sorted_iter(&v, |a, b| a == b));
assert_eq!(false, contains_duplicates_in_sorted_iter(&v, |a, b| a == b));
}

#[test]
Expand Down Expand Up @@ -3005,24 +3008,20 @@ mod tests {
<Test as frame_system::Config>::BlockNumber,
>>::deduplicate_and_sort_dispute_data(&mut disputes).unwrap_err();

use core::cmp::Ordering;

// assert ordering of local only disputes, and at the same time, and being free of duplicates
assert_eq!(disputes_orig.len(), disputes.len() + 1);

// assert ordering of local only disputes
disputes.windows(2).for_each(|window| {
let a = window[0].clone();
let b = window[1].clone();
let are_these_equal = |a: &DisputeStatementSet, b: &DisputeStatementSet| {
use core::cmp::Ordering;
// we only have local disputes here, so sorting of those adheres to the
// simplified sorting logic
let session_cmp = a.session.cmp(&b.session);
let cmp = if session_cmp == Ordering::Equal {
b.candidate_hash.cmp(&b.candidate_hash)
} else {
session_cmp
};
let cmp =
a.session.cmp(&b.session).then_with(|| a.candidate_hash.cmp(&b.candidate_hash));
assert_ne!(cmp, Ordering::Greater);
});
cmp == Ordering::Equal
};

assert_eq!(false, contains_duplicates_in_sorted_iter(&disputes, are_these_equal));
})
}

Expand Down Expand Up @@ -3573,7 +3572,7 @@ mod tests {
}

#[test]
fn assure_detects_duplicate_statements_sets() {
fn assure_no_duplicate_statements_sets_are_fine() {
new_test_ext(Default::default()).execute_with(|| {
let v0 = <ValidatorId as CryptoType>::Pair::generate().0;
let v1 = <ValidatorId as CryptoType>::Pair::generate().0;
Expand Down Expand Up @@ -3620,33 +3619,93 @@ mod tests {
),
];

let mut sets = vec![
let sets = vec![
DisputeStatementSet {
candidate_hash: candidate_hash_a.clone(),
session: 1,
statements: statements.clone(),
},
DisputeStatementSet {
candidate_hash: candidate_hash_a.clone(),
session: 1,
session: 2,
statements: statements.clone(),
},
];

// `Err(())` indicates presence of duplicates
assert!(<Pallet::<Test> as DisputesHandler<
<Test as frame_system::Config>::BlockNumber,
>>::assure_deduplicated_and_sorted(&mut sets)
.is_err());
>>::assure_deduplicated_and_sorted(&sets)
.is_ok());
})
}

assert_eq!(
sets,
vec![DisputeStatementSet {
#[test]
fn assure_detects_duplicate_statements_sets() {
new_test_ext(Default::default()).execute_with(|| {
let v0 = <ValidatorId as CryptoType>::Pair::generate().0;
let v1 = <ValidatorId as CryptoType>::Pair::generate().0;

run_to_block(3, |b| {
// a new session at each block
Some((
true,
b,
vec![(&0, v0.public()), (&1, v1.public())],
Some(vec![(&0, v0.public()), (&1, v1.public())]),
))
});

let candidate_hash_a = CandidateHash(sp_core::H256::repeat_byte(1));

let payload = ExplicitDisputeStatement {
valid: true,
candidate_hash: candidate_hash_a.clone(),
session: 1,
}
.signing_payload();

let payload_against = ExplicitDisputeStatement {
valid: false,
candidate_hash: candidate_hash_a.clone(),
session: 1,
}
.signing_payload();

let sig_a = v0.sign(&payload);
let sig_a_against = v1.sign(&payload_against);

let statements = vec![
(
DisputeStatement::Valid(ValidDisputeStatementKind::Explicit),
ValidatorIndex(0),
sig_a.clone(),
),
(
DisputeStatement::Invalid(InvalidDisputeStatementKind::Explicit),
ValidatorIndex(1),
sig_a_against.clone(),
),
];

let sets = vec![
DisputeStatementSet {
candidate_hash: candidate_hash_a.clone(),
session: 1,
statements,
}]
);
statements: statements.clone(),
},
DisputeStatementSet {
candidate_hash: candidate_hash_a.clone(),
session: 1,
statements: statements.clone(),
},
];

// `Err(())` indicates presence of duplicates
assert!(<Pallet::<Test> as DisputesHandler<
<Test as frame_system::Config>::BlockNumber,
>>::assure_deduplicated_and_sorted(&sets)
.is_err());
})
}

Expand Down