Skip to content

Commit

Permalink
node: fix verify_failed_iterations
Browse files Browse the repository at this point in the history
Ensure the function returns true if all failed iterations (according to the candidate block iteration) have a quorum.
Fixed a bug where the function incorrectly returned true if no failed attestations were added to the list and the candidate iteration was non-zero.
  • Loading branch information
herr-seppia committed Jun 19, 2024
1 parent 334ad73 commit 6f81f4a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions node/src/chain/header_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,18 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
Ok(())
}

/// Return true if there is a attestation for each failed iteration, and if
/// Return true if there is an attestation for each failed iteration, and if
/// that attestation has a quorum in the ratification phase.
///
/// If there are no failed iterations, it returns true
pub async fn verify_failed_iterations(
&self,
candidate_block: &'a ledger::Header,
) -> anyhow::Result<bool> {
// Verify Failed iterations
let mut all_failed = true;
// Iteration is 0 based. Eg: having a candidate with iter=2 means we
// should expect 2 failed attestation for iter=0 and iter=1
let expected_failed_atts = candidate_block.iteration;
let mut actual_failed_atts = 0u8;

for (iter, att) in candidate_block
.failed_iterations
Expand All @@ -194,7 +196,7 @@ impl<'a, DB: database::DB> Validator<'a, DB> {

anyhow::ensure!(pk == &expected_pk, "Invalid generator. Expected {expected_pk:?}, actual {pk:?}");

let quorums = verify_block_att(
let (_, rat_quorum) = verify_block_att(
self.prev_header.hash,
self.prev_header.seed,
self.provisioners.current(),
Expand All @@ -204,14 +206,13 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
)
.await?;

// Ratification quorum is enough to consider the iteration
// failed
all_failed = all_failed && quorums.1.quorum_reached();
} else {
all_failed = false;
if rat_quorum.quorum_reached() {
actual_failed_atts += 1;
}
}
}

let all_failed = actual_failed_atts == expected_failed_atts;
Ok(all_failed)
}

Expand Down

0 comments on commit 6f81f4a

Please sign in to comment.