Skip to content

Commit

Permalink
node: refactor verify_success_att
Browse files Browse the repository at this point in the history
  • Loading branch information
fed-franz committed Jul 23, 2024
1 parent 9a138a5 commit 39576f9
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions node/src/chain/header_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use dusk_consensus::quorum::verifiers::QuorumResult;
use dusk_consensus::user::committee::{Committee, CommitteeSet};
use dusk_consensus::user::provisioners::{ContextProvisioners, Provisioners};
use execution_core::stake::EPOCH;
use node_data::ledger::{to_str, Fault, InvalidFault, Seed, Signature};
use node_data::ledger::{to_str, Fault, Hash, InvalidFault, Seed, Signature};
use node_data::message::payload::{RatificationResult, Vote};
use node_data::message::ConsensusHeader;
use node_data::{ledger, StepName};
Expand Down Expand Up @@ -62,17 +62,23 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
pub async fn execute_checks(
&self,
candidate_block: &ledger::Header,
disable_winner_att_check: bool,
) -> anyhow::Result<(u8, Vec<Voter>, Vec<Voter>)>
{
disable_att_check: bool,
) -> anyhow::Result<(u8, Vec<Voter>, Vec<Voter>)> {
self.verify_basic_fields(candidate_block).await?;

let prev_block_voters =
self.verify_prev_block_cert(candidate_block).await?;

let mut candidate_block_voters = vec![];
if !disable_winner_att_check {
candidate_block_voters =
self.verify_success_att(candidate_block).await?;
if !disable_att_check {
candidate_block_voters = verify_success_att(
&att,
candidate_block.get_consensus_header(),
self.prev_header.seed,
self.provisioners.current(),
candidate_block.hash,
)
.await?;
}

let pni = self.verify_failed_iterations(candidate_block).await?;
Expand Down Expand Up @@ -167,28 +173,20 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
return Ok(vec![]);
}

let cert_result = candidate_block.prev_block_cert.result;
let prev_block_hash = candidate_block.prev_block_hash;

match candidate_block.prev_block_cert.result {
RatificationResult::Success(Vote::Valid(hash))
if hash == prev_block_hash => {}
_ => anyhow::bail!(
"Invalid result for previous block hash: {cert_result:?}"
),
}

let prev_block_seed = self.db.read().await.view(|v| {
v.fetch_block_header(&self.prev_header.prev_block_hash)?
.ok_or_else(|| anyhow::anyhow!("Header not found"))
.map(|h| h.seed)
})?;

let (_, _, voters) = verify_att(
let (_, _, voters) = verify_success_att(
&candidate_block.prev_block_cert,
self.prev_header.get_consensus_header(),
prev_block_seed,
self.provisioners.prev(),
prev_block_hash,
)
.await?;

Expand Down Expand Up @@ -245,13 +243,28 @@ impl<'a, DB: database::DB> Validator<'a, DB> {

pub async fn verify_success_att(
&self,
candidate_block: &'a ledger::Header,
att: &'a ledger::Attestation,
consensus_header: ConsensusHeader,
curr_seed: Signature,
curr_eligible_provisioners: &Provisioners,
block_hash: Hash,
) -> anyhow::Result<Vec<Voter>> {
let cert_result = att.result;
let prev_block_hash = candidate_block.prev_block_hash;

match att.result {
RatificationResult::Success(Vote::Valid(hash))
if hash == block_hash => {}
_ => anyhow::bail!(
"Invalid result for previous block hash: {cert_result:?}"
),
}

let (_, _, voters) = verify_att(
&candidate_block.att,
candidate_block.get_consensus_header(),
self.prev_header.seed,
self.provisioners.current(),
&att,
consensus_header,
curr_seed,
curr_eligible_provisioners,
)
.await?;

Expand Down

0 comments on commit 39576f9

Please sign in to comment.