Skip to content

Commit

Permalink
fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Sep 5, 2024
1 parent 9d71404 commit 7ebafad
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 180 deletions.
6 changes: 5 additions & 1 deletion chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ pub trait ChainReader {
fn current_tips_hash(&self) -> Result<Vec<HashValue>>;
fn has_dag_block(&self, header_id: HashValue) -> Result<bool>;
fn check_chain_type(&self) -> Result<ChainType>;
fn verify_and_ghostdata(&self, uncles: &[BlockHeader], header: &BlockHeader) -> Result<GhostdagData>;
fn verify_and_ghostdata(
&self,
uncles: &[BlockHeader],
header: &BlockHeader,
) -> Result<GhostdagData>;
}

pub trait ChainWriter {
Expand Down
24 changes: 15 additions & 9 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,14 @@ impl BlockChain {
.get_block_header_by_hash(self.genesis_hash)?
.ok_or_else(|| format_err!("failed to get genesis because it is none"))?;
let result = match verified_block.ghostdata {
Some(trusted_ghostdata) => {
self.dag.commit_trusted_block(header.to_owned(), genesis_header.parent_hash(), Arc::new(trusted_ghostdata))
}
None => {
self.dag.commit(header.to_owned(), genesis_header.parent_hash())
}
Some(trusted_ghostdata) => self.dag.commit_trusted_block(
header.to_owned(),
genesis_header.parent_hash(),
Arc::new(trusted_ghostdata),
),
None => self
.dag
.commit(header.to_owned(), genesis_header.parent_hash()),
};
match result {
anyhow::Result::Ok(_) => info!("finish to commit dag block: {:?}", block_id),
Expand Down Expand Up @@ -1352,8 +1354,12 @@ impl ChainReader for BlockChain {
fn check_chain_type(&self) -> Result<ChainType> {
Ok(ChainType::Dag)
}

fn verify_and_ghostdata(&self, uncles: &[BlockHeader], header: &BlockHeader) -> Result<starcoin_dag::types::ghostdata::GhostdagData> {

fn verify_and_ghostdata(
&self,
uncles: &[BlockHeader],
header: &BlockHeader,
) -> Result<starcoin_dag::types::ghostdata::GhostdagData> {
self.dag().verify_and_ghostdata(uncles, header)
}
}
Expand Down Expand Up @@ -1540,7 +1546,7 @@ impl ChainWriter for BlockChain {
fn chain_state(&mut self) -> &ChainStateDB {
&self.statedb
}

fn apply_for_sync(&mut self, block: Block) -> Result<ExecutedBlock> {
self.apply_with_verifier::<DagVerifierWithGhostData>(block)
}
Expand Down
28 changes: 17 additions & 11 deletions chain/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub trait BlockVerifier {
new_block_header,
)?;
watch(CHAIN_WATCH_NAME, "n14");
Ok(VerifiedBlock{
Ok(VerifiedBlock {
block: new_block,
ghostdata,
})
Expand Down Expand Up @@ -322,10 +322,10 @@ impl BlockVerifier for NoneVerifier {
where
R: ChainReader,
{
Ok(VerifiedBlock{
Ok(VerifiedBlock {
block: new_block,
ghostdata: None,
})
})
}

fn verify_uncles<R>(
Expand All @@ -340,7 +340,6 @@ impl BlockVerifier for NoneVerifier {
}
}


struct BasicDagVerifier;
impl BasicDagVerifier {
pub fn verify_header<R>(current_chain: &R, new_block_header: &BlockHeader) -> Result<()>
Expand Down Expand Up @@ -386,13 +385,17 @@ impl BasicDagVerifier {

ConsensusVerifier::verify_header(current_chain, new_block_header)
}

fn verify_blue_blocks<R>(current_chain: &R, uncles: &[BlockHeader], header: &BlockHeader) -> Result<GhostdagData> where R: ChainReader {

fn verify_blue_blocks<R>(
current_chain: &R,
uncles: &[BlockHeader],
header: &BlockHeader,
) -> Result<GhostdagData>
where
R: ChainReader,
{
current_chain.verify_and_ghostdata(uncles, header)
}



}
//TODO: Implement it.
pub struct DagVerifier;
Expand All @@ -416,7 +419,6 @@ impl BlockVerifier for DagVerifier {
}
}


pub struct DagVerifierWithGhostData;
impl BlockVerifier for DagVerifierWithGhostData {
fn verify_header<R>(current_chain: &R, new_block_header: &BlockHeader) -> Result<()>
Expand All @@ -434,6 +436,10 @@ impl BlockVerifier for DagVerifierWithGhostData {
where
R: ChainReader,
{
Ok(Some(BasicDagVerifier::verify_blue_blocks(current_chain, uncles, header)?))
Ok(Some(BasicDagVerifier::verify_blue_blocks(
current_chain,
uncles,
header,
)?))
}
}
63 changes: 47 additions & 16 deletions flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::consensusdb::{
};
use crate::ghostdag::protocol::GhostdagManager;
use crate::prune::pruning_point_manager::PruningPointManagerT;
use crate::types::ghostdata::CompactGhostdagData;
use crate::{process_key_already_error, reachability};
use anyhow::{bail, ensure, format_err, Ok};
use starcoin_accumulator::node::AccumulatorStoreType;
Expand All @@ -26,7 +25,6 @@ use starcoin_types::{
blockhash::{BlockHashes, KType},
consensus_header::ConsensusHeader,
};
use std::char::EscapeUnicode;
use std::collections::HashSet;
use std::ops::DerefMut;
use std::sync::Arc;
Expand Down Expand Up @@ -145,7 +143,12 @@ impl BlockDAG {
Ok(())
}

pub fn commit_trusted_block(&mut self, header: BlockHeader, origin: HashValue, trusted_ghostdata: Arc<GhostdagData>) -> anyhow::Result<()> {
pub fn commit_trusted_block(
&mut self,
header: BlockHeader,
origin: HashValue,
trusted_ghostdata: Arc<GhostdagData>,
) -> anyhow::Result<()> {
info!(
"start to commit header: {:?}, number: {:?}",
header.id(),
Expand All @@ -165,16 +168,39 @@ impl BlockDAG {
if header.is_genesis() {
Arc::new(self.ghostdag_manager.genesis_ghostdag_data(&header))
} else {
self.storage.ghost_dag_store.insert(header.id(), trusted_ghostdata.clone())?;
self.storage
.ghost_dag_store
.insert(header.id(), trusted_ghostdata.clone())?;
trusted_ghostdata
}
}
Some(ghostdata) => {
ensure!(ghostdata.blue_score == trusted_ghostdata.blue_score, "blue score is not same");
ensure!(ghostdata.blue_work == trusted_ghostdata.blue_work, "blue work is not same");
ensure!(ghostdata.mergeset_blues.len() == trusted_ghostdata.mergeset_blues.len(), "blue len is not same");
ensure!(ghostdata.mergeset_blues.iter().cloned().collect::<HashSet<_>>() == trusted_ghostdata.mergeset_blues.iter().cloned().collect::<HashSet<_>>(), "blue values are not same");
trusted_ghostdata
ensure!(
ghostdata.blue_score == trusted_ghostdata.blue_score,
"blue score is not same"
);
ensure!(
ghostdata.blue_work == trusted_ghostdata.blue_work,
"blue work is not same"
);
ensure!(
ghostdata.mergeset_blues.len() == trusted_ghostdata.mergeset_blues.len(),
"blue len is not same"
);
ensure!(
ghostdata
.mergeset_blues
.iter()
.cloned()
.collect::<HashSet<_>>()
== trusted_ghostdata
.mergeset_blues
.iter()
.cloned()
.collect::<HashSet<_>>(),
"blue values are not same"
);
trusted_ghostdata
}
};
// Store ghostdata
Expand Down Expand Up @@ -265,8 +291,6 @@ impl BlockDAG {
Ok(())
}



pub fn commit(&mut self, header: BlockHeader, origin: HashValue) -> anyhow::Result<()> {
info!(
"start to commit header: {:?}, number: {:?}",
Expand Down Expand Up @@ -541,12 +565,19 @@ impl BlockDAG {

anyhow::Ok(())
}

pub fn reachability_store(&self) -> Arc<parking_lot::lock_api::RwLock<parking_lot::RawRwLock, DbReachabilityStore>> {

pub fn reachability_store(
&self,
) -> Arc<parking_lot::lock_api::RwLock<parking_lot::RawRwLock, DbReachabilityStore>> {
self.storage.reachability_store.clone()
}

pub fn verify_and_ghostdata(&self, blue_blocks: &[BlockHeader], header: &BlockHeader) -> Result<GhostdagData, anyhow::Error> {
self.ghost_dag_manager().verify_and_ghostdata(blue_blocks, header)

pub fn verify_and_ghostdata(
&self,
blue_blocks: &[BlockHeader],
header: &BlockHeader,
) -> Result<GhostdagData, anyhow::Error> {
self.ghost_dag_manager()
.verify_and_ghostdata(blue_blocks, header)
}
}
Loading

0 comments on commit 7ebafad

Please sign in to comment.