Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: fix failed_certificates validation #1261

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,10 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
/// prev_block is usually the blockchain tip
pub(crate) async fn verify_block_header<DB: database::DB>(
db: Arc<RwLock<DB>>,
prev_block: &ledger::Header,
prev_header: &ledger::Header,
provisioners: &ContextProvisioners,
header: &ledger::Header,
) -> anyhow::Result<bool> {
let validator = Validator::new(db, prev_block, provisioners);
let validator = Validator::new(db, prev_header, provisioners);
validator.execute_checks(header, false).await
}
30 changes: 15 additions & 15 deletions node/src/chain/header_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ use tracing::info;
/// according to current context
pub(crate) struct Validator<'a, DB: database::DB> {
pub(crate) db: Arc<RwLock<DB>>,
block: &'a ledger::Header,
prev_header: &'a ledger::Header,
provisioners: &'a ContextProvisioners,
}

impl<'a, DB: database::DB> Validator<'a, DB> {
pub fn new(
db: Arc<RwLock<DB>>,
block: &'a ledger::Header,
prev_header: &'a ledger::Header,
provisioners: &'a ContextProvisioners,
) -> Self {
Self {
db,
block,
prev_header,
provisioners,
}
}
Expand Down Expand Up @@ -71,15 +71,15 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
return Err(anyhow!("empty block hash"));
}

if candidate_block.height != self.block.height + 1 {
if candidate_block.height != self.prev_header.height + 1 {
return Err(anyhow!(
"invalid block height block_height: {:?}, curr_height: {:?}",
candidate_block.height,
self.block.height,
self.prev_header.height,
));
}

if candidate_block.prev_block_hash != self.block.hash {
if candidate_block.prev_block_hash != self.prev_header.hash {
return Err(anyhow!("invalid previous block hash"));
}

Expand All @@ -99,13 +99,13 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
&self,
candidate_block: &'a ledger::Header,
) -> anyhow::Result<()> {
if self.block.height == 0 {
if self.prev_header.height == 0 {
return Ok(());
}

let prev_block_seed = self.db.read().await.view(|v| {
let prev_block =
Ledger::fetch_block_by_height(&v, self.block.height - 1)?
Ledger::fetch_block_by_height(&v, self.prev_header.height - 1)?
.ok_or_else(|| anyhow::anyhow!("could not fetch block"))?;

Ok::<_, anyhow::Error>(prev_block.header().seed)
Expand All @@ -114,10 +114,10 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
verify_block_cert(
prev_block_seed,
self.provisioners.prev(),
self.block.hash,
self.block.height,
self.prev_header.hash,
self.prev_header.height,
&candidate_block.prev_block_cert,
self.block.iteration,
self.prev_header.iteration,
true,
)
.await?;
Expand All @@ -141,16 +141,16 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
if let Some((cert, pk)) = cert {
info!(event = "verify_cert", cert_type = "failed_cert", iter);
let expected_pk = self.provisioners.current().get_generator(
candidate_block.iteration,
candidate_block.seed,
iter as u8,
self.prev_header.seed,
candidate_block.height,
);
if pk != &expected_pk {
anyhow::bail!("Invalid generator. Expected {expected_pk:?}, actual {pk:?}");
}

let quorums = verify_block_cert(
self.block.seed,
self.prev_header.seed,
self.provisioners.current(),
[0u8; 32],
candidate_block.height,
Expand All @@ -176,7 +176,7 @@ impl<'a, DB: database::DB> Validator<'a, DB> {
candidate_block: &'a ledger::Header,
) -> anyhow::Result<()> {
verify_block_cert(
self.block.seed,
self.prev_header.seed,
self.provisioners.current(),
candidate_block.hash,
candidate_block.height,
Expand Down