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

Check dag parents' existence #4144

Merged
merged 1 commit into from
Jun 21, 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 chain/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::verifier::{BlockVerifier, DagBasicVerifier, DagVerifier, FullVerifier};
use crate::verifier::{BlockVerifier, DagVerifier, FullVerifier};
use anyhow::{anyhow, bail, ensure, format_err, Ok, Result};
use once_cell::sync::Lazy;
use sp_utils::stop_watch::{watch, CHAIN_WATCH_NAME};
Expand Down Expand Up @@ -1967,7 +1967,7 @@ impl ChainReader for BlockChain {
match self.check_chain_type()? {
ChainType::Single => FullVerifier::verify_block(self, block),
ChainType::Dag => {
DagBasicVerifier::verify_header(self, block.header())?;
DagVerifier::verify_header(self, block.header())?;
Ok(VerifiedBlock(block))
}
}
Expand Down
65 changes: 20 additions & 45 deletions chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ impl BlockVerifier for DagVerifier {
new_block_header.parent_hash()
);

parents_hash_to_check.iter().try_for_each(|parent_hash| {
verify_block!(
VerifyBlockField::Header,
current_chain.has_dag_block(*parent_hash).map_err(|e| {
ConnectBlockError::VerifyBlockFailed(
VerifyBlockField::Header,
anyhow::anyhow!(
"failed to get the block: {:?} 's parent: {:?} from db, error: {:?}",
new_block_header.id(),
parent_hash,
e
),
)
})?,
"Invalid block: parent {} might not exist.",
parent_hash
);
Ok::<(), ConnectBlockError>(())
})?;

ConsensusVerifier::verify_header(current_chain, new_block_header)
}

Expand Down Expand Up @@ -424,48 +444,3 @@ impl BlockVerifier for DagVerifier {
Ok(())
}
}

//TODO: Implement it.
pub struct DagBasicVerifier;
impl BlockVerifier for DagBasicVerifier {
fn verify_uncles<R>(
_current_chain: &R,
_uncles: &[BlockHeader],
_header: &BlockHeader,
) -> Result<()>
where
R: ChainReader,
{
Ok(())
}
fn verify_header<R>(current_chain: &R, new_block_header: &BlockHeader) -> Result<()>
where
R: ChainReader,
{
let parents_hash = new_block_header.parents_hash().unwrap_or_default();
let mut parents_hash_to_check = parents_hash.clone();
parents_hash_to_check.sort();
parents_hash_to_check.dedup();

verify_block!(
VerifyBlockField::Header,
!parents_hash_to_check.is_empty() && parents_hash.len() == parents_hash_to_check.len(),
"Invalid parents_hash in dag basic verifier {:?} for a dag block {}",
new_block_header.parents_hash(),
new_block_header.number(),
);

verify_block!(
VerifyBlockField::Header,
parents_hash_to_check.contains(&new_block_header.parent_hash())
&& current_chain
.get_block_info(Some(new_block_header.parent_hash()))?
.is_some(),
"Invalid block: parent {} might not exist.",
new_block_header.parent_hash()
);

Ok(())
// ConsensusVerifier::verify_header(current_chain, new_block_header)
}
}
4 changes: 2 additions & 2 deletions sync/src/block_connector/write_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ where
#[cfg(test)]
pub fn apply_failed(&mut self, block: Block) -> Result<()> {
use anyhow::bail;
use starcoin_chain::verifier::{DagBasicVerifier, FullVerifier};
use starcoin_chain::verifier::{DagVerifier, FullVerifier};

let verified_block = match self.main.check_chain_type()? {
ChainType::Single => {
Expand All @@ -254,7 +254,7 @@ where
}
ChainType::Dag => {
// apply but no connection
self.main.verify_with_verifier::<DagBasicVerifier>(block)?
self.main.verify_with_verifier::<DagVerifier>(block)?
}
};
let _executed_block = self.main.execute(verified_block)?;
Expand Down
8 changes: 4 additions & 4 deletions sync/src/tasks/block_sync_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::FutureExt;
use network_api::PeerId;
use network_api::PeerProvider;
use starcoin_accumulator::{Accumulator, MerkleAccumulator};
use starcoin_chain::verifier::DagBasicVerifier;
use starcoin_chain::verifier::DagVerifier;
use starcoin_chain::{verifier::BasicVerifier, BlockChain};
use starcoin_chain_api::{ChainReader, ChainType, ChainWriter, ConnectBlockError, ExecutedBlock};
use starcoin_config::G_CRATE_VERSION;
Expand Down Expand Up @@ -489,7 +489,7 @@ where
} else {
let executed_block = self
.chain
.apply_with_verifier::<DagBasicVerifier>(block.clone())?;
.apply_with_verifier::<DagVerifier>(block.clone())?;
info!(
"succeed to apply a dag block: {:?}, number: {:?}",
executed_block.block.id(),
Expand Down Expand Up @@ -603,7 +603,7 @@ where
{
let executed_block = self
.chain
.apply_with_verifier::<DagBasicVerifier>(child_block.block.clone())?;
.apply_with_verifier::<DagVerifier>(child_block.block.clone())?;
info!(
"succeed to apply a dag block: {:?}, number: {:?}",
executed_block.block.id(),
Expand Down Expand Up @@ -669,7 +669,7 @@ where
{
let executed_block = self
.chain
.apply_with_verifier::<DagBasicVerifier>(child_block.block.clone())?;
.apply_with_verifier::<DagVerifier>(child_block.block.clone())?;
info!(
"succeed to apply a dag block: {:?}, number: {:?}",
executed_block.block.id(),
Expand Down
Loading