Grandpa justifications: Avoid duplicate vote ancestries (#2634) (#2635)

* Grandpa justifications: Avoid duplicate vote ancestries
This commit is contained in:
Serban Iorga
2023-10-19 08:16:40 +03:00
committed by Bastian Köcher
parent 0067c9fa7c
commit dcd2debbb2
7 changed files with 114 additions and 16 deletions
@@ -42,7 +42,7 @@ struct AncestryChain(bp_header_chain::justification::AncestryChain<TestHeader>);
impl AncestryChain {
fn new(justification: &GrandpaJustification<TestHeader>) -> Self {
Self(bp_header_chain::justification::AncestryChain::new(justification))
Self(bp_header_chain::justification::AncestryChain::new(justification).0)
}
}
@@ -58,7 +58,7 @@ impl finality_grandpa::Chain<TestHash, TestNumber> for AncestryChain {
if current_hash == base {
break
}
match self.0.parents.get(&current_hash) {
match self.0.parent_hash_of(&current_hash) {
Some(parent_hash) => {
current_hash = *parent_hash;
route.push(current_hash);
@@ -158,6 +158,26 @@ fn unrelated_ancestry_votes_are_removed_by_optimizer() {
assert_eq!(num_precommits_before - 1, num_precommits_after);
}
#[test]
fn duplicate_votes_ancestries_are_removed_by_optimizer() {
let mut justification = make_default_justification::<TestHeader>(&test_header(1));
let optimized_votes_ancestries = justification.votes_ancestries.clone();
justification.votes_ancestries = justification
.votes_ancestries
.into_iter()
.flat_map(|item| std::iter::repeat(item).take(3))
.collect();
verify_and_optimize_justification::<TestHeader>(
header_id::<TestHeader>(1),
&verification_context(TEST_GRANDPA_SET_ID),
&mut justification,
)
.unwrap();
assert_eq!(justification.votes_ancestries, optimized_votes_ancestries);
}
#[test]
fn redundant_votes_ancestries_are_removed_by_optimizer() {
let mut justification = make_default_justification::<TestHeader>(&test_header(1));
@@ -149,7 +149,21 @@ fn justification_with_invalid_authority_signature_rejected() {
}
#[test]
fn justification_with_invalid_precommit_ancestry() {
fn justification_with_duplicate_votes_ancestry() {
let mut justification = make_default_justification::<TestHeader>(&test_header(1));
justification.votes_ancestries.push(justification.votes_ancestries[0].clone());
assert_eq!(
verify_justification::<TestHeader>(
header_id::<TestHeader>(1),
&verification_context(TEST_GRANDPA_SET_ID),
&justification,
),
Err(JustificationVerificationError::DuplicateVotesAncestries),
);
}
#[test]
fn justification_with_redundant_votes_ancestry() {
let mut justification = make_default_justification::<TestHeader>(&test_header(1));
justification.votes_ancestries.push(test_header(10));