Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
sanlee42 committed Sep 26, 2023
1 parent c8556ab commit 6dfd421
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
29 changes: 11 additions & 18 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use std::cmp::min;
use std::iter::Extend;
use std::option::Option::{None, Some};
use std::{collections::HashMap, sync::Arc};
use starcoin_consensus::dag::types::ghostdata::GhostdagData;

pub struct ChainStatusWithBlock {
pub status: ChainStatus,
Expand All @@ -65,7 +66,6 @@ pub struct BlockChain {
epoch: Epoch,
vm_metrics: Option<VMMetrics>,
dag_accumulator: Option<MerkleAccumulator>,
dag: BlockDAG,
}

impl BlockChain {
Expand All @@ -74,12 +74,11 @@ impl BlockChain {
head_block_hash: HashValue,
storage: Arc<dyn Store>,
vm_metrics: Option<VMMetrics>,
dag: BlockDAG,
) -> Result<Self> {
let head = storage
.get_block_by_hash(head_block_hash)?
.ok_or_else(|| format_err!("Can not find block by hash {:?}", head_block_hash))?;
Self::new_with_uncles(time_service, head, None, storage, vm_metrics, dag)
Self::new_with_uncles(time_service, head, None, storage, vm_metrics)
}

fn new_with_uncles(
Expand All @@ -88,7 +87,6 @@ impl BlockChain {
uncles: Option<HashMap<HashValue, MintedUncleNumber>>,
storage: Arc<dyn Store>,
vm_metrics: Option<VMMetrics>,
dag: BlockDAG,
) -> Result<Self> {
let block_info = storage
.get_block_info(head_block.id())?
Expand Down Expand Up @@ -139,7 +137,6 @@ impl BlockChain {
epoch,
vm_metrics,
dag_accumulator,
dag,
};
watch(CHAIN_WATCH_NAME, "n1251");
match uncles {
Expand All @@ -155,7 +152,6 @@ impl BlockChain {
storage: Arc<dyn Store>,
genesis_epoch: Epoch,
genesis_block: Block,
dag_store: FlexiDagStorage,
) -> Result<Self> {
debug_assert!(genesis_block.header().is_genesis());
let txn_accumulator = MerkleAccumulator::new_empty(
Expand Down Expand Up @@ -191,9 +187,7 @@ impl BlockChain {
new_tips,
dag_accumulator.get_info(),
)?;
let mut dag = BlockDAG::new(genesis_id, 16, dag_store);
dag.init_with_genesis(DagHeader::new_genesis(genesis_header))?;
Self::new(time_service, executed_block.block.id(), storage, None, dag)
Self::new(time_service, executed_block.block.id(), storage, None)
}

pub fn current_epoch_uncles_size(&self) -> u64 {
Expand Down Expand Up @@ -371,8 +365,8 @@ impl BlockChain {
}

pub fn verify_with_verifier<V>(&mut self, block: Block) -> Result<VerifiedBlock>
where
V: BlockVerifier,
where
V: BlockVerifier,
{
V::verify_block(self, block)
}
Expand All @@ -383,8 +377,8 @@ impl BlockChain {
dag_block_parent: Option<HashValue>,
next_tips: &mut Option<Vec<HashValue>>,
) -> Result<ExecutedBlock>
where
V: BlockVerifier,
where
V: BlockVerifier,
{
let verified_block = self.verify_with_verifier::<V>(block)?;
watch(CHAIN_WATCH_NAME, "n1");
Expand Down Expand Up @@ -660,9 +654,9 @@ impl BlockChain {

return Ok(next_tips_info
== self
.dag_accumulator
.as_ref()
.map(|accumulator| accumulator.get_info()));
.dag_accumulator
.as_ref()
.map(|accumulator| accumulator.get_info()));
}
}

Expand Down Expand Up @@ -887,7 +881,6 @@ impl ChainReader for BlockChain {
self.storage.clone(),
self.vm_metrics.clone(),
//TODO: check missing blocks need to be clean
self.dag.clone(),
)
}

Expand Down Expand Up @@ -1155,7 +1148,7 @@ impl ChainWriter for BlockChain {
.expect("dag blocks must have tips")
.clone(),
)
.expect("failed to calculate the tips hash")
.expect("failed to calculate the tips hash")
== executed_block.block().header().parent_hash();
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/db-exporter/src/verify_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ impl BatchCmdExec<VerifyHeaderCmdType, Block, VerifyHeaderError> for Block {
}
}
}
}
}
18 changes: 12 additions & 6 deletions consensus/src/dag/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::ghostdag::protocol::{ColoringOutput, GhostdagManager};
use super::reachability::{inquirer, reachability_service::MTReachabilityService};
use super::types::ghostdata::GhostdagData;
use crate::consensusdb::prelude::StoreError;
use crate::consensusdb::schemadb::GhostdagStoreReader;
use crate::consensusdb::{
prelude::FlexiDagStorage,
schemadb::{
Expand Down Expand Up @@ -80,10 +81,11 @@ impl BlockDAG {
self.relations_store
.insert(Hash::new(ORIGIN), BlockHashes::new(vec![]))
.unwrap();
self.commit_header(genesis)
let _ = self.addToDag(genesis);
Ok(())
}

pub fn commit_header(&mut self, header: DagHeader) -> anyhow::Result<()> {
pub fn addToDag(&mut self, header: DagHeader) -> anyhow::Result<GhostdagData> {
//TODO:check genesis
// Generate ghostdag data
let parents_hash = header.parents_hash();
Expand Down Expand Up @@ -117,7 +119,7 @@ impl BlockDAG {
let _ = self
.header_store
.insert(header.hash(), Arc::new(header.to_owned()), 0)?;
Ok(())
return Ok(ghostdag_data.clone())
}

fn is_in_dag(&self, _hash: Hash) -> anyhow::Result<bool> {
Expand All @@ -134,7 +136,7 @@ impl BlockDAG {
if is_orphan_block {
return Ok(());
}
self.commit_header(header.clone());
self.addToDag(header.clone());
self.check_missing_block(header)?;
Ok(())
}
Expand All @@ -144,7 +146,7 @@ impl BlockDAG {
for orphan in orphans.iter() {
let is_orphan = self.is_orphan(&orphan)?;
if !is_orphan {
self.commit_header(header.clone());
self.addToDag(header.clone());
}
}
}
Expand All @@ -158,6 +160,10 @@ impl BlockDAG {
}
return Ok(true);
}
pub fn get_ghostdag_data(&self, hash: Hash)->anyhow::Result<Arc<GhostdagData>>{
let ghostdata = self.ghostdag_store.get_data(hash)?;
return Ok(ghostdata)
}

fn update_orphans(&mut self, block_header: &DagHeader) -> anyhow::Result<bool> {
let mut is_orphan = false;
Expand Down Expand Up @@ -249,6 +255,6 @@ mod tests {
let mut dag = BlockDAG::new(genesis_hash, k, db);
dag.init_with_genesis(genesis);
let block = DagHeader::new(BlockHeader::random(), vec![genesis_hash]);
dag.commit_header(block);
dag.addToDag(block);
}
}
27 changes: 21 additions & 6 deletions sync/src/block_connector/write_block_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ use starcoin_types::{
};
use std::fmt::Formatter;
use std::sync::{Arc, Mutex};
use starcoin_types::header::DagHeader;
use crate::block_connector::write_block_chain::ConnectOk::{DagConnected, ExeConnectMain};

const MAX_ROLL_BACK_BLOCK: usize = 10;

pub struct WriteBlockChainService<P>
where
P: TxPoolSyncService,
where
P: TxPoolSyncService,
{
config: Arc<NodeConfig>,
startup_info: StartupInfo,
Expand Down Expand Up @@ -92,8 +94,8 @@ impl std::fmt::Display for ConnectOk {
}

impl<P> WriteableChainService for WriteBlockChainService<P>
where
P: TxPoolSyncService + 'static,
where
P: TxPoolSyncService + 'static,
{
fn try_connect(&mut self, block: Block, tips_headers: Option<Vec<HashValue>>) -> Result<()> {
let _timer = self
Expand Down Expand Up @@ -124,8 +126,8 @@ where
}

impl<P> WriteBlockChainService<P>
where
P: TxPoolSyncService + 'static,
where
P: TxPoolSyncService + 'static,
{
pub fn new(
config: Arc<NodeConfig>,
Expand Down Expand Up @@ -791,6 +793,19 @@ where
return Ok(ConnectOk::ExeConnectMain(executed_block));
}

fn connect_dag_inner(&mut self, block: Block, parents_hash: Vec<HashValue>,
) -> Result<ConnectOk> {
let ghost_dag_data = self.dag.lock().unwrap().addToDag(DagHeader::new(block.header, parents_hash))?;
let past_header = ghost_dag_data.selected_parent;
let mut chain = self.main.fork(past_header)?;
for blue_hash in ghost_dag_data.mergeset_blues{
chain.apply(blue_hash);
}

//self.broadcast_new_head();
Ok(DagConnected)
}

fn connect_inner(
&mut self,
block: Block,
Expand Down
7 changes: 1 addition & 6 deletions types/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,7 @@ impl Block {
.map(|uncles| uncles.len() as u64)
.unwrap_or(0);

let parent = if dag_block_parent.is_some() {
dag_block_parent.unwrap()
} else {
self.header.parent_hash()
};

let parent = dag_block_parent.unwrap_or(self.header.parent_hash());
BlockMetadata::new(
parent,
self.header.timestamp,
Expand Down

0 comments on commit 6dfd421

Please sign in to comment.